Skip to content

Commit 42cf186

Browse files
authoredDec 9, 2021
feat(ec2): propagate EC2 tags to volumes (#17840)
aws-cloudformation/cloudformation-coverage-roadmap#133 just shipped. Docs: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-propagatetagstovolumeoncreation Waiting on cloudfromation specs to get bumped to the latest version. Depends on #17844. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0b80db5 commit 42cf186

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
 

‎packages/@aws-cdk/aws-ec2/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,23 @@ instance.userData.addCommands(
10731073
);
10741074
```
10751075

1076+
#### Tagging Volumes
1077+
1078+
You can configure [tag propagation on volume creation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-propagatetagstovolumeoncreation).
1079+
1080+
```ts
1081+
declare const vpc: ec2.Vpc;
1082+
declare const instanceType: ec2.InstanceType;
1083+
declare const machineImage: ec2.IMachineImage;
1084+
1085+
new ec2.Instance(this, 'Instance', {
1086+
vpc,
1087+
machineImage,
1088+
instanceType,
1089+
propagateTagsToVolumeOnCreation: true,
1090+
});
1091+
```
1092+
10761093
### Configuring Instance Metadata Service (IMDS)
10771094

10781095
#### Toggling IMDSv1

‎packages/@aws-cdk/aws-ec2/lib/instance.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ export interface InstanceProps {
214214
*
215215
* @default - no association
216216
*/
217-
readonly privateIpAddress?: string
217+
readonly privateIpAddress?: string;
218+
219+
/**
220+
* Propagate the EC2 instance tags to the EBS volumes.
221+
*
222+
* @default - false
223+
*/
224+
readonly propagateTagsToVolumeOnCreation?: boolean;
218225

219226
/**
220227
* Apply the given CloudFormation Init configuration to the instance at startup
@@ -373,6 +380,7 @@ export class Instance extends Resource implements IInstance {
373380
sourceDestCheck: props.sourceDestCheck,
374381
blockDeviceMappings: props.blockDevices !== undefined ? instanceBlockDeviceMappings(this, props.blockDevices) : undefined,
375382
privateIpAddress: props.privateIpAddress,
383+
propagateTagsToVolumeOnCreation: props.propagateTagsToVolumeOnCreation,
376384
});
377385
this.instance.node.addDependency(this.role);
378386

‎packages/@aws-cdk/aws-ec2/test/instance.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ describe('instance', () => {
196196
}
197197

198198

199+
});
200+
test('can propagate EBS volume tags', () => {
201+
// WHEN
202+
new Instance(stack, 'Instance', {
203+
vpc,
204+
machineImage: new AmazonLinuxImage(),
205+
instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.LARGE),
206+
propagateTagsToVolumeOnCreation: true,
207+
});
208+
209+
// THEN
210+
expect(stack).toHaveResource('AWS::EC2::Instance', {
211+
PropagateTagsToVolumeOnCreation: true,
212+
});
199213
});
200214
describe('blockDeviceMappings', () => {
201215
test('can set blockDeviceMappings', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.