Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ec2): add versionDescription property for LaunchTemplate #30837

Merged
merged 8 commits into from
Jul 24, 2024

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@
}
]
}
]
],
"VersionDescription": "test template v1"
}
},
"sg2860DD91F": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const sg1 = new ec2.SecurityGroup(stack, 'sg1', {
});

const lt = new ec2.LaunchTemplate(stack, 'LT', {
versionDescription: 'test template v1',
httpEndpoint: true,
httpProtocolIpv6: true,
httpPutResponseHopLimit: 2,
Expand Down
2 changes: 2 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,8 @@ const instanceProfile = new iam.InstanceProfile(this, 'InstanceProfile', {
});

const template = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
launchTemplateName: 'MyTemplateV1',
versionDescription: 'This is my v1 template',
machineImage: ec2.MachineImage.latestAmazonLinux2023(),
securityGroup: new ec2.SecurityGroup(this, 'LaunchTemplateSG', {
vpc: vpc,
Expand Down
16 changes: 16 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/lib/launch-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ export interface LaunchTemplateProps {
*/
readonly launchTemplateName?: string;

/**
* A description for the first version of the launch template.
*
* The version description must be maximum 255 characters long.
*
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-launchtemplate.html#cfn-ec2-launchtemplate-versiondescription
*
* @default - No description
*/
readonly versionDescription?: string;

/**
* Type of instance to launch.
*
Expand Down Expand Up @@ -735,8 +746,13 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
? [{ deviceIndex: 0, associatePublicIpAddress: props.associatePublicIpAddress, groups: securityGroupsToken }]
: undefined;

if (props.versionDescription && !Token.isUnresolved(props.versionDescription) && props.versionDescription.length > 255) {
throw new Error(`versionDescription must be less than or equal to 255 characters, got ${props.versionDescription.length}`);
}

const resource = new CfnLaunchTemplate(this, 'Resource', {
launchTemplateName: props?.launchTemplateName,
versionDescription: props?.versionDescription,
launchTemplateData: {
blockDeviceMappings: props?.blockDevices !== undefined ? launchTemplateBlockDeviceMappings(this, props.blockDevices) : undefined,
creditSpecification: props?.cpuCredits !== undefined ? {
Expand Down
22 changes: 22 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/test/launch-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ describe('LaunchTemplate', () => {
});
});

test('Given versionDescription', () => {
// WHEN
new LaunchTemplate(stack, 'Template', {
versionDescription: 'test template',
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::EC2::LaunchTemplate', {
VersionDescription: 'test template',
});
});

test('throw error when versionDescription is too long', () => {
const tooLongDescription = 'a'.repeat(256);
// WHEN / THEN
expect(() => {
new LaunchTemplate(stack, 'TemplateWithTooLongDescription', {
versionDescription: tooLongDescription,
});
}).toThrow('versionDescription must be less than or equal to 255 characters, got 256');
});

test('Given instanceType', () => {
// WHEN
new LaunchTemplate(stack, 'Template', {
Expand Down