Skip to content

Commit bb4311b

Browse files
authored
fix(ec2): userData in launchTemplate is created automatically when machineImege is provided (#23593)
closes #23592 Reading through the discussion in PR #12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design [here](#12385 (comment)), but it seems it accidentally became out of scope at the time. This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior. * Users who already sets a userData explicitly: * will not see any change to their synthesized template, as this PR only modifies userData when it is not set explicitly. * Users who is not using userData: * will see a userData is added to their template. But the userData is empty and does nothing. It should not have any effect on the previous behavior. ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 686c72d commit bb4311b

25 files changed

+379
-170
lines changed

packages/@aws-cdk/aws-autoscaling/test/integ.asg-lt.js.snapshot/aws-cdk-asg-integ.assets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "22.0.0",
33
"files": {
4-
"768b8ff8b1178a04dbfca488da9459f4f402bfad643db0b4791787ef23ec4db5": {
4+
"2f0699c7863c01ee4fb7ccca4b192b311d9d6723f012b40f36bdf25a5db22f0c": {
55
"source": {
66
"path": "aws-cdk-asg-integ.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "768b8ff8b1178a04dbfca488da9459f4f402bfad643db0b4791787ef23ec4db5.json",
12+
"objectKey": "2f0699c7863c01ee4fb7ccca4b192b311d9d6723f012b40f36bdf25a5db22f0c.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk/aws-autoscaling/test/integ.asg-lt.js.snapshot/aws-cdk-asg-integ.template.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
}
2828
]
2929
}
30-
]
30+
],
31+
"UserData": {
32+
"Fn::Base64": "#!/bin/bash"
33+
}
3134
},
3235
"TagSpecifications": [
3336
{
@@ -69,7 +72,10 @@
6972
}
7073
]
7174
}
72-
]
75+
],
76+
"UserData": {
77+
"Fn::Base64": "#!/bin/bash"
78+
}
7379
},
7480
"TagSpecifications": [
7581
{

packages/@aws-cdk/aws-autoscaling/test/integ.asg-lt.js.snapshot/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"validateOnSynth": false,
1818
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
1919
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/768b8ff8b1178a04dbfca488da9459f4f402bfad643db0b4791787ef23ec4db5.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/2f0699c7863c01ee4fb7ccca4b192b311d9d6723f012b40f36bdf25a5db22f0c.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [

packages/@aws-cdk/aws-autoscaling/test/integ.asg-lt.js.snapshot/tree.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
}
4343
]
4444
}
45-
]
45+
],
46+
"userData": {
47+
"Fn::Base64": "#!/bin/bash"
48+
}
4649
},
4750
"tagSpecifications": [
4851
{
@@ -118,7 +121,10 @@
118121
}
119122
]
120123
}
121-
]
124+
],
125+
"userData": {
126+
"Fn::Base64": "#!/bin/bash"
127+
}
122128
},
123129
"tagSpecifications": [
124130
{
@@ -1275,7 +1281,7 @@
12751281
"path": "Tree",
12761282
"constructInfo": {
12771283
"fqn": "constructs.Construct",
1278-
"version": "10.1.168"
1284+
"version": "10.1.189"
12791285
}
12801286
}
12811287
},

packages/@aws-cdk/aws-ec2/lib/launch-template.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
TagType,
1313
Tags,
1414
Token,
15+
FeatureFlags,
1516
} from '@aws-cdk/core';
17+
import * as cxapi from '@aws-cdk/cx-api';
1618
import { Construct } from 'constructs';
1719
import { Connections, IConnectable } from './connections';
1820
import { CfnLaunchTemplate } from './ec2.generated';
@@ -591,8 +593,19 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
591593
},
592594
});
593595

594-
if (props.userData) {
595-
this.userData = props.userData;
596+
const imageConfig: MachineImageConfig | undefined = props.machineImage?.getImage(this);
597+
if (imageConfig) {
598+
this.osType = imageConfig.osType;
599+
this.imageId = imageConfig.imageId;
600+
}
601+
602+
if (FeatureFlags.of(this).isEnabled(cxapi.EC2_LAUNCH_TEMPLATE_DEFAULT_USER_DATA)) {
603+
// priority: prop.userData -> userData from machineImage -> undefined
604+
this.userData = props.userData ?? imageConfig?.userData;
605+
} else {
606+
if (props.userData) {
607+
this.userData = props.userData;
608+
}
596609
}
597610
const userDataToken = Lazy.string({
598611
produce: () => {
@@ -603,12 +616,6 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
603616
},
604617
});
605618

606-
const imageConfig: MachineImageConfig | undefined = props.machineImage?.getImage(this);
607-
if (imageConfig) {
608-
this.osType = imageConfig.osType;
609-
this.imageId = imageConfig.imageId;
610-
}
611-
612619
this.instanceType = props.instanceType;
613620

614621
let marketOptions: any = undefined;

packages/@aws-cdk/aws-ec2/test/integ.launch-template.js.snapshot/LambdaTestDefaultTestDeployAssert1AF2B360.assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "21.0.0",
2+
"version": "22.0.0",
33
"files": {
44
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
55
"source": {

packages/@aws-cdk/aws-ec2/test/integ.launch-template.js.snapshot/TestStack.assets.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/@aws-cdk/aws-ec2/test/integ.launch-template.js.snapshot/TestStack.template.json

Lines changed: 0 additions & 83 deletions
This file was deleted.

packages/@aws-cdk/aws-ec2/test/integ.launch-template.js.snapshot/aws-cdk-ec2-lt-metadata-1.assets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"version": "21.0.0",
2+
"version": "22.0.0",
33
"files": {
4-
"ea313fee581c8e898c158e9d123dd48345192689bb08a3f7e84716db61247c6c": {
4+
"534a1fbecaccb7e2a071086c8085be5c15b2501781767cdeddf754fe3a0ceecb": {
55
"source": {
66
"path": "aws-cdk-ec2-lt-metadata-1.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "ea313fee581c8e898c158e9d123dd48345192689bb08a3f7e84716db61247c6c.json",
12+
"objectKey": "534a1fbecaccb7e2a071086c8085be5c15b2501781767cdeddf754fe3a0ceecb.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk/aws-ec2/test/integ.launch-template.js.snapshot/aws-cdk-ec2-lt-metadata-1.template.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,57 @@
4444
}
4545
]
4646
}
47+
},
48+
"LTWithMachineImageAAC227A5": {
49+
"Type": "AWS::EC2::LaunchTemplate",
50+
"Properties": {
51+
"LaunchTemplateData": {
52+
"ImageId": {
53+
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
54+
},
55+
"TagSpecifications": [
56+
{
57+
"ResourceType": "instance",
58+
"Tags": [
59+
{
60+
"Key": "Name",
61+
"Value": "aws-cdk-ec2-lt-metadata-1/LTWithMachineImage"
62+
}
63+
]
64+
},
65+
{
66+
"ResourceType": "volume",
67+
"Tags": [
68+
{
69+
"Key": "Name",
70+
"Value": "aws-cdk-ec2-lt-metadata-1/LTWithMachineImage"
71+
}
72+
]
73+
}
74+
],
75+
"UserData": {
76+
"Fn::Base64": "#!/bin/bash"
77+
}
78+
},
79+
"TagSpecifications": [
80+
{
81+
"ResourceType": "launch-template",
82+
"Tags": [
83+
{
84+
"Key": "Name",
85+
"Value": "aws-cdk-ec2-lt-metadata-1/LTWithMachineImage"
86+
}
87+
]
88+
}
89+
]
90+
}
4791
}
4892
},
4993
"Parameters": {
94+
"SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter": {
95+
"Type": "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>",
96+
"Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
97+
},
5098
"BootstrapVersion": {
5199
"Type": "AWS::SSM::Parameter::Value<String>",
52100
"Default": "/cdk-bootstrap/hnb659fds/version",

0 commit comments

Comments
 (0)