Skip to content

Commit

Permalink
fix(cfn-include): NestedStack's Parameters are not converted to strings
Browse files Browse the repository at this point in the history
When we include a NestedStack when creating a CfnInclude instance,
the conversion of the Parameters property of the AWS::CloudFormation::Stack resource is performed manually
(because of the eventuality that one of those Parameters was also requested to be removed when including the nested stack).
In that manual conversion, we did not correctly convert the values to strings,
which is what the underlying CfnStack class expects.
And so, if the parent stack passed a non-string primitive value to a nested stack Parameter,
like a number or boolean,
including the nested stack would fail with a validation exception.

Fixes aws#15092
  • Loading branch information
skinny85 committed Jun 14, 2021
1 parent cbd7552 commit 3209874
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export class CfnInclude extends core.CfnElement {
return nestedStackResource;
}

private parametersForNestedStack(parameters: any, nestedStackId: string): { [key: string]: any } | undefined {
private parametersForNestedStack(parameters: any, nestedStackId: string): { [key: string]: string } | undefined {
if (parameters == null) {
return undefined;
}
Expand All @@ -703,7 +703,7 @@ export class CfnInclude extends core.CfnElement {
const ret: { [key: string]: string } = {};
for (const paramName of Object.keys(parameters)) {
if (!(paramName in parametersToReplace)) {
ret[paramName] = parameters[paramName];
ret[paramName] = cfn_parse.FromCloudFormation.getString(parameters[paramName]).value;
}
}
return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,28 @@ describe('CDK Include for nested stacks', () => {
}).toThrow(/Nested Stack 'AnotherChildStack' was not included in the parent template/);
});

test('correctly handles references in nested stacks Parameters', () => {
new inc.CfnInclude(stack, 'ParentStack', {
templateFile: testTemplateFilePath('cross-stack-refs.json'),
loadNestedStacks: {
'ChildStack': {
templateFile: testTemplateFilePath('child-import-stack.json'),
},
},
});

expect(stack).toHaveResourceLike('AWS::CloudFormation::Stack', {
"Parameters": {
"Param1": {
"Ref": "Param",
},
"Param2": {
"Fn::GetAtt": ["Bucket", "Arn"],
},
},
});
});

test('correctly handles renaming of references across nested stacks', () => {
const parentTemplate = new inc.CfnInclude(stack, 'ParentStack', {
templateFile: testTemplateFilePath('cross-stack-refs.json'),
Expand Down Expand Up @@ -424,6 +446,23 @@ describe('CDK Include for nested stacks', () => {
});
});

test('can ingest a NestedStack with a Number CFN Parameter passed as a number', () => {
new inc.CfnInclude(stack, 'MyScope', {
templateFile: testTemplateFilePath('parent-number-in-child-params.yaml'),
loadNestedStacks: {
'NestedStack': {
templateFile: testTemplateFilePath('child-with-number-parameter.yaml'),
},
},
});

expect(stack).toHaveResourceLike('AWS::CloudFormation::Stack', {
"Parameters": {
"Number": "60",
},
});
});

test('can lazily include a single child nested stack', () => {
const parentTemplate = new inc.CfnInclude(stack, 'ParentStack', {
templateFile: testTemplateFilePath('parent-one-child.json'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
Number:
Type: Number
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: 'testbucket1234unique'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
AWSTemplateFormatVersion: '2010-09-09'
Resources:
NestedStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: 'https://s3.amazonaws.com/masonme-cdk-test/templates/nested-bucket.yaml'
Parameters:
Number: 60

0 comments on commit 3209874

Please sign in to comment.