Skip to content

Commit

Permalink
fix(ec2): invalid volume type check for iops (#19073)
Browse files Browse the repository at this point in the history
IO2 and GP3 volumes can also specify `iops`

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
hoegertn authored Feb 28, 2022
1 parent 427cdfd commit 3f49f02
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-ec2/lib/private/ebs-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ function synthesizeBlockDeviceMappings<RT, NDT>(construct: Construct, blockDevic
const { iops, volumeType, kmsKey, ...rest } = ebs;

if (!iops) {
if (volumeType === EbsDeviceVolumeType.IO1) {
throw new Error('iops property is required with volumeType: EbsDeviceVolumeType.IO1');
if (volumeType === EbsDeviceVolumeType.IO1 || volumeType === EbsDeviceVolumeType.IO2) {
throw new Error('iops property is required with volumeType: EbsDeviceVolumeType.IO1 and EbsDeviceVolumeType.IO2');
}
} else if (volumeType !== EbsDeviceVolumeType.IO1) {
Annotations.of(construct).addWarning('iops will be ignored without volumeType: EbsDeviceVolumeType.IO1');
} else if (volumeType !== EbsDeviceVolumeType.IO1 && volumeType !== EbsDeviceVolumeType.IO2 && volumeType !== EbsDeviceVolumeType.GP3) {
Annotations.of(construct).addWarning('iops will be ignored without volumeType: IO1, IO2, or GP3');
}

/**
Expand Down
48 changes: 40 additions & 8 deletions packages/@aws-cdk/aws-ec2/test/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ describe('instance', () => {
volumeType: EbsDeviceVolumeType.IO1,
iops: 5000,
}),
}, {
deviceName: 'ebs-gp3',
mappingEnabled: true,
volume: BlockDeviceVolume.ebs(15, {
deleteOnTermination: true,
encrypted: true,
volumeType: EbsDeviceVolumeType.GP3,
iops: 5000,
}),
}, {
deviceName: 'ebs-cmk',
mappingEnabled: true,
Expand Down Expand Up @@ -236,6 +245,16 @@ describe('instance', () => {
VolumeType: 'io1',
},
},
{
DeviceName: 'ebs-gp3',
Ebs: {
DeleteOnTermination: true,
Encrypted: true,
Iops: 5000,
VolumeSize: 15,
VolumeType: 'gp3',
},
},
{
DeviceName: 'ebs-cmk',
Ebs: {
Expand Down Expand Up @@ -306,8 +325,25 @@ describe('instance', () => {
}],
});
}).toThrow(/ops property is required with volumeType: EbsDeviceVolumeType.IO1/);
});


test('throws if volumeType === IO2 without iops', () => {
// THEN
expect(() => {
new Instance(stack, 'Instance', {
vpc,
machineImage: new AmazonLinuxImage(),
instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.LARGE),
blockDevices: [{
deviceName: 'ebs',
volume: BlockDeviceVolume.ebs(15, {
deleteOnTermination: true,
encrypted: true,
volumeType: EbsDeviceVolumeType.IO2,
}),
}],
});
}).toThrow(/ops property is required with volumeType: EbsDeviceVolumeType.IO1 and EbsDeviceVolumeType.IO2/);
});

test('warning if iops without volumeType', () => {
Expand All @@ -327,12 +363,10 @@ describe('instance', () => {

// THEN
expect(instance.node.metadataEntry[0].type).toEqual(cxschema.ArtifactMetadataEntryType.WARN);
expect(instance.node.metadataEntry[0].data).toEqual('iops will be ignored without volumeType: EbsDeviceVolumeType.IO1');


expect(instance.node.metadataEntry[0].data).toEqual('iops will be ignored without volumeType: IO1, IO2, or GP3');
});

test('warning if iops and volumeType !== IO1', () => {
test('warning if iops and invalid volumeType', () => {
const instance = new Instance(stack, 'Instance', {
vpc,
machineImage: new AmazonLinuxImage(),
Expand All @@ -350,9 +384,7 @@ describe('instance', () => {

// THEN
expect(instance.node.metadataEntry[0].type).toEqual(cxschema.ArtifactMetadataEntryType.WARN);
expect(instance.node.metadataEntry[0].data).toEqual('iops will be ignored without volumeType: EbsDeviceVolumeType.IO1');


expect(instance.node.metadataEntry[0].data).toEqual('iops will be ignored without volumeType: IO1, IO2, or GP3');
});
});

Expand Down

0 comments on commit 3f49f02

Please sign in to comment.