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): propagate EC2 tags to volumes #17840

Merged
merged 7 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-ec2/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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