Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cfn-include): detect a resource cycle in the included template #19871

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,12 @@ export class CfnInclude extends core.CfnElement {
return cfnCondition;
}

private getOrCreateResource(logicalId: string): core.CfnResource {
private getOrCreateResource(logicalId: string, cycleChain: string[] = []): core.CfnResource {
cycleChain = cycleChain.concat([logicalId]);
if (cycleChain.length !== new Set(cycleChain).size) {
throw new Error(`Found a cycle between resources in the template: ${cycleChain.join(' depends on ')}`);
}

const ret = this.resources[logicalId];
if (ret) {
return ret;
Expand Down Expand Up @@ -618,7 +623,7 @@ export class CfnInclude extends core.CfnElement {
if (!(lId in (self.template.Resources || {}))) {
return undefined;
}
return self.getOrCreateResource(lId);
return self.getOrCreateResource(lId, cycleChain);
},

findRefTarget(elementName: string): core.CfnElement | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ describe('CDK Include', () => {
includeTestTemplate(stack, 'short-form-get-att-no-dot.yaml');
}).toThrow(/Short-form Fn::GetAtt must contain a '.' in its string argument, got: 'Bucket1Arn'/);
});

test('detects a cycle between resources in the template', () => {
expect(() => {
includeTestTemplate(stack, 'cycle-in-resources.json');
}).toThrow(/Found a cycle between resources in the template: Bucket1 depends on Bucket2 depends on Bucket1/);
});
});

function includeTestTemplate(scope: constructs.Construct, testTemplate: string): inc.CfnInclude {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Resources": {
"Bucket1": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": {
"Ref": "Bucket2"
}
}
},
"Bucket2": {
"Type": "AWS::S3::Bucket",
"DependsOn": "Bucket1"
}
}
}