Skip to content

Commit b31580a

Browse files
authored
fix(ec2): validate throughput property on EC2 instance block devices (#34571)
### Issue # (if applicable) Closes #34033. ### Reason for this change The throughput property for GP3 volumes is not supported on EC2 instances and only works with Launch Templates. This causes confusion for users who try to set throughput on EC2 instance block devices, as the property is silently ignored. ### Description of changes Added synthesis-time validation in the instanceBlockDeviceMappings function to detect when throughput is specified for an EBS volume on an EC2 instance. The validation throws a clear error message directing users to use Launch Templates instead. ### Describe any new or updated permissions being added No new or updated IAM permissions are needed. ### Description of how you validated changes Added a unit test that verifies the validation works correctly by attempting to create an EC2 instance with a GP3 volume that has throughput specified and confirming that the expected error message is thrown. ### 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*
1 parent 802f130 commit b31580a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/aws-cdk-lib/aws-ec2/lib/private/ebs-util.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import { CfnInstance, CfnLaunchTemplate } from '../ec2.generated';
44
import { BlockDevice, EbsDeviceVolumeType } from '../volume';
55

66
export function instanceBlockDeviceMappings(construct: Construct, blockDevices: BlockDevice[]): CfnInstance.BlockDeviceMappingProperty[] {
7+
for (const blockDevice of blockDevices) {
8+
if (blockDevice.volume.ebsDevice?.throughput !== undefined) {
9+
Annotations.of(construct).addWarningV2('@aws-cdk/aws-ec2:throughputNotSupported',
10+
'The throughput property is not supported on EC2 instances. Use a Launch Template instead. ' +
11+
'See https://github.com/aws/aws-cdk/issues/34033 for more information.',
12+
);
13+
}
14+
}
715
return synthesizeBlockDeviceMappings<CfnInstance.BlockDeviceMappingProperty, object>(construct, blockDevices, {});
816
}
917

packages/aws-cdk-lib/aws-ec2/test/instance.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,26 @@ describe('instance', () => {
347347
});
348348
});
349349

350+
test('warns if throughput is specified for an EBS volume', () => {
351+
// WHEN
352+
new Instance(stack, 'Instance', {
353+
vpc,
354+
machineImage: new AmazonLinuxImage(),
355+
instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.LARGE),
356+
blockDevices: [{
357+
deviceName: 'ebs',
358+
volume: BlockDeviceVolume.ebs(15, {
359+
deleteOnTermination: true,
360+
encrypted: true,
361+
volumeType: EbsDeviceVolumeType.GP3,
362+
throughput: 125,
363+
}),
364+
}],
365+
});
366+
// THEN
367+
Annotations.fromStack(stack).hasWarning('/Default/Instance', Match.stringLikeRegexp('The throughput property is not supported on EC2 instances. Use a Launch Template instead'));
368+
});
369+
350370
test('throws if ephemeral volumeIndex < 0', () => {
351371
// THEN
352372
expect(() => {

0 commit comments

Comments
 (0)