Skip to content

Commit

Permalink
feat(ec2): add versionDescription property for LaunchTemplate (#3…
Browse files Browse the repository at this point in the history
…0837)

### 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*
  • Loading branch information
yuppe-kyupeen committed Jul 24, 2024
1 parent f5dd73b commit 597228c
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 57 deletions.

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

0 comments on commit 597228c

Please sign in to comment.