diff --git a/packages/@aws-cdk/aws-ec2/README.md b/packages/@aws-cdk/aws-ec2/README.md index f673e24111e9f..16c999efde724 100644 --- a/packages/@aws-cdk/aws-ec2/README.md +++ b/packages/@aws-cdk/aws-ec2/README.md @@ -1073,6 +1073,23 @@ instance.userData.addCommands( ); ``` +#### Tagging Volumes + +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). + +```ts + declare const vpc: ec2.Vpc; + declare const instanceType: ec2.InstanceType; + declare const machineImage: ec2.IMachineImage; + + new ec2.Instance(this, 'Instance', { + vpc, + machineImage, + instanceType, + propagateTagsToVolumeOnCreation: true, + }); +``` + ### Configuring Instance Metadata Service (IMDS) #### Toggling IMDSv1 diff --git a/packages/@aws-cdk/aws-ec2/lib/instance.ts b/packages/@aws-cdk/aws-ec2/lib/instance.ts index 85b05fc71734b..a12c3a45d6108 100644 --- a/packages/@aws-cdk/aws-ec2/lib/instance.ts +++ b/packages/@aws-cdk/aws-ec2/lib/instance.ts @@ -214,7 +214,14 @@ export interface InstanceProps { * * @default - no association */ - readonly privateIpAddress?: string + readonly privateIpAddress?: string; + + /** + * Propagate the EC2 instance tags to the EBS volumes. + * + * @default - false + */ + readonly propagateTagsToVolumeOnCreation?: boolean; /** * Apply the given CloudFormation Init configuration to the instance at startup @@ -373,6 +380,7 @@ export class Instance extends Resource implements IInstance { sourceDestCheck: props.sourceDestCheck, blockDeviceMappings: props.blockDevices !== undefined ? instanceBlockDeviceMappings(this, props.blockDevices) : undefined, privateIpAddress: props.privateIpAddress, + propagateTagsToVolumeOnCreation: props.propagateTagsToVolumeOnCreation, }); this.instance.node.addDependency(this.role); diff --git a/packages/@aws-cdk/aws-ec2/test/instance.test.ts b/packages/@aws-cdk/aws-ec2/test/instance.test.ts index a53092b6a41d4..86326ef7242df 100644 --- a/packages/@aws-cdk/aws-ec2/test/instance.test.ts +++ b/packages/@aws-cdk/aws-ec2/test/instance.test.ts @@ -196,6 +196,20 @@ describe('instance', () => { } + }); + test('can propagate EBS volume tags', () => { + // WHEN + new Instance(stack, 'Instance', { + vpc, + machineImage: new AmazonLinuxImage(), + instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.LARGE), + propagateTagsToVolumeOnCreation: true, + }); + + // THEN + expect(stack).toHaveResource('AWS::EC2::Instance', { + PropagateTagsToVolumeOnCreation: true, + }); }); describe('blockDeviceMappings', () => { test('can set blockDeviceMappings', () => {