Skip to content

Commit

Permalink
feat(ec2): propagate EC2 tags to volumes (#17840)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
robertd committed Dec 9, 2021
1 parent 0b80db5 commit 42cf186
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-ec2/README.md
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion packages/@aws-cdk/aws-ec2/lib/instance.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/instance.test.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 42cf186

Please sign in to comment.