-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ec2: userData in launchTemplate is not created automatically even when machineImege is provided #23592
Description
Describe the bug
Reading this doc, userData is expected to be automatically created when machineImage is provided.
(optional, default: This Launch Template creates a UserData based on the type of provided machineImage; no UserData is created if a machineImage is not provided)
However, it is not working as expected; userData is not automatically created at all. See reproduction steps for the actual code.
Expected Behavior
userData is automatically created when machineImage is provided.
Current Behavior
userData is not created even when machineImage is provided.
Reproduction Steps
Synthesize the below stack:
export class Ec2UserdataStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const launchTemplate = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
instanceType: new ec2.InstanceType('t3.small'),
machineImage: new ec2.AmazonLinuxImage({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
}),
});
// this will cause an error
launchTemplate.userData!.addCommands("yum -y update")
}
}and we get the following error:
/../ec2-userdata/lib/ec2-userdata-stack.ts:26
launchTemplate.userData!.addCommands("yum -y update")
^
TypeError: Cannot read properties of undefined (reading 'addCommands')
Possible Solution
Current implementation is here. We need to add code for creating userData depending on machineImage.
I guess we can follow the below implementation in ec2.Instance:
aws-cdk/packages/@aws-cdk/aws-ec2/lib/instance.ts
Lines 349 to 351 in f178c98
| // use delayed evaluation | |
| const imageConfig = props.machineImage.getImage(this); | |
| this.userData = props.userData ?? imageConfig.userData; |
Additional Information/Context
Adding this feature will not be a breaking change because we are just adding empty userData to launchTemplates whose userData was not specified explicitly, and it will not have any effect on the existing behavior.
CDK CLI Version
2.59.0
Framework Version
2.59.0
Node.js Version
v16.18.1
OS
macOS
Language
Typescript
Language Version
No response
Other information
No response