Skip to content

Commit d038b61

Browse files
authored
fix(cfn-include): no longer concatenate elements of Fn::Join without tokens (#9476)
Fn.join() has some logic inside of it to simplify the expressions to concatenate the array elements that do not contain any Tokens inside of them. We don't want to do it in cfn-include though, as that causes a diff from the original template. So, wrap the array given as the second argument to Fn::Join into a Token, to prevent the concatenation logic from triggering. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7b29774 commit d038b61

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"Resources": {
3+
"LaunchConfig": {
4+
"Type" : "AWS::AutoScaling::LaunchConfiguration",
5+
"Properties": {
6+
"ImageId": "ami-01e24be29428c15b2",
7+
"InstanceType": "t1.micro",
8+
"UserData": {
9+
"Fn::Base64": {
10+
"Fn::Join": ["", [
11+
"#!/bin/bash -xe\n",
12+
"yum update -y aws-cfn-bootstrap\n",
13+
14+
"/opt/aws/bin/cfn-init -v ",
15+
" --stack ", { "Ref" : "AWS::StackName" },
16+
" --resource LaunchConfig ",
17+
" --configsets wordpress_install ",
18+
" --region ", { "Ref" : "AWS::Region" }, "\n",
19+
20+
"/opt/aws/bin/cfn-signal -e $? ",
21+
" --stack ", { "Ref" : "AWS::StackName" },
22+
" --resource WebServerGroup ",
23+
" --region ", { "Ref" : "AWS::Region" }, "\n"
24+
]]
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}

packages/@aws-cdk/cloudformation-include/test/valid-templates.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ describe('CDK Include', () => {
184184
);
185185
});
186186

187+
test('can ingest a UserData script, and output it unchanged', () => {
188+
includeTestTemplate(stack, 'user-data.json');
189+
190+
expect(stack).toMatchTemplate(
191+
loadTestFileToJsObject('user-data.json'),
192+
);
193+
});
194+
187195
test('can ingest a template with intrinsic functions and conditions, and output it unchanged', () => {
188196
includeTestTemplate(stack, 'functions-and-conditions.json');
189197

packages/@aws-cdk/core/lib/cfn-parse.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from './cfn-resource-policy';
88
import { CfnTag } from './cfn-tag';
99
import { ICfnFinder } from './from-cfn';
10+
import { Lazy } from './lazy';
1011
import { CfnReference } from './private/cfn-reference';
1112
import { IResolvable } from './resolvable';
1213
import { isResolvableObject, Token } from './token';
@@ -359,7 +360,11 @@ export class CfnParser {
359360
// where the first element is the delimiter,
360361
// and the second is the list of elements to join
361362
const value = this.parseValue(object[key]);
362-
return Fn.join(value[0], value[1]);
363+
// wrap the array as a Token,
364+
// as otherwise Fn.join() will try to concatenate
365+
// the non-token parts,
366+
// causing a diff with the original template
367+
return Fn.join(value[0], Lazy.listValue({ produce: () => value[1] }));
363368
}
364369
case 'Fn::Cidr': {
365370
const value = this.parseValue(object[key]);

0 commit comments

Comments
 (0)