Skip to content

Commit 597228c

Browse files
feat(ec2): add versionDescription property for LaunchTemplate (#30837)
### Issue # (if applicable) ### Reason for this change The change introduces the `versionDescription` property to the `LaunchTemplate` ### Description of changes - Add the `versionDescription` property for `LaunchTemplateProps`, which was missing in the L2 construct. - Add validation for character limit ### Description of how you validated changes I Added a unit test for launch template and added the `versionDescription` property in the integration tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent f5dd73b commit 597228c

File tree

8 files changed

+100
-57
lines changed

8 files changed

+100
-57
lines changed

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

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@
211211
}
212212
]
213213
}
214-
]
214+
],
215+
"VersionDescription": "test template v1"
215216
}
216217
},
217218
"sg2860DD91F": {

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.launch-template.js.snapshot/manifest.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.launch-template.js.snapshot/tree.json

+54-53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.launch-template.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const sg1 = new ec2.SecurityGroup(stack, 'sg1', {
1616
});
1717

1818
const lt = new ec2.LaunchTemplate(stack, 'LT', {
19+
versionDescription: 'test template v1',
1920
httpEndpoint: true,
2021
httpProtocolIpv6: true,
2122
httpPutResponseHopLimit: 2,

packages/aws-cdk-lib/aws-ec2/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,8 @@ const instanceProfile = new iam.InstanceProfile(this, 'InstanceProfile', {
22872287
});
22882288

22892289
const template = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
2290+
launchTemplateName: 'MyTemplateV1',
2291+
versionDescription: 'This is my v1 template',
22902292
machineImage: ec2.MachineImage.latestAmazonLinux2023(),
22912293
securityGroup: new ec2.SecurityGroup(this, 'LaunchTemplateSG', {
22922294
vpc: vpc,

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

+16
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ export interface LaunchTemplateProps {
221221
*/
222222
readonly launchTemplateName?: string;
223223

224+
/**
225+
* A description for the first version of the launch template.
226+
*
227+
* The version description must be maximum 255 characters long.
228+
*
229+
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-launchtemplate.html#cfn-ec2-launchtemplate-versiondescription
230+
*
231+
* @default - No description
232+
*/
233+
readonly versionDescription?: string;
234+
224235
/**
225236
* Type of instance to launch.
226237
*
@@ -735,8 +746,13 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
735746
? [{ deviceIndex: 0, associatePublicIpAddress: props.associatePublicIpAddress, groups: securityGroupsToken }]
736747
: undefined;
737748

749+
if (props.versionDescription && !Token.isUnresolved(props.versionDescription) && props.versionDescription.length > 255) {
750+
throw new Error(`versionDescription must be less than or equal to 255 characters, got ${props.versionDescription.length}`);
751+
}
752+
738753
const resource = new CfnLaunchTemplate(this, 'Resource', {
739754
launchTemplateName: props?.launchTemplateName,
755+
versionDescription: props?.versionDescription,
740756
launchTemplateData: {
741757
blockDeviceMappings: props?.blockDevices !== undefined ? launchTemplateBlockDeviceMappings(this, props.blockDevices) : undefined,
742758
creditSpecification: props?.cpuCredits !== undefined ? {

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

+22
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,28 @@ describe('LaunchTemplate', () => {
147147
});
148148
});
149149

150+
test('Given versionDescription', () => {
151+
// WHEN
152+
new LaunchTemplate(stack, 'Template', {
153+
versionDescription: 'test template',
154+
});
155+
156+
// THEN
157+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::LaunchTemplate', {
158+
VersionDescription: 'test template',
159+
});
160+
});
161+
162+
test('throw error when versionDescription is too long', () => {
163+
const tooLongDescription = 'a'.repeat(256);
164+
// WHEN / THEN
165+
expect(() => {
166+
new LaunchTemplate(stack, 'TemplateWithTooLongDescription', {
167+
versionDescription: tooLongDescription,
168+
});
169+
}).toThrow('versionDescription must be less than or equal to 255 characters, got 256');
170+
});
171+
150172
test('Given instanceType', () => {
151173
// WHEN
152174
new LaunchTemplate(stack, 'Template', {

0 commit comments

Comments
 (0)