From 3f49f020090142c77feb892894c54e62dc4de7ae Mon Sep 17 00:00:00 2001 From: Thorsten Hoeger Date: Mon, 28 Feb 2022 17:05:48 +0100 Subject: [PATCH] fix(ec2): invalid volume type check for iops (#19073) 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* --- .../@aws-cdk/aws-ec2/lib/private/ebs-util.ts | 8 ++-- .../@aws-cdk/aws-ec2/test/instance.test.ts | 48 +++++++++++++++---- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/packages/@aws-cdk/aws-ec2/lib/private/ebs-util.ts b/packages/@aws-cdk/aws-ec2/lib/private/ebs-util.ts index 52c7738afdbef..d66dc01f31398 100644 --- a/packages/@aws-cdk/aws-ec2/lib/private/ebs-util.ts +++ b/packages/@aws-cdk/aws-ec2/lib/private/ebs-util.ts @@ -31,11 +31,11 @@ function synthesizeBlockDeviceMappings(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'); } /** diff --git a/packages/@aws-cdk/aws-ec2/test/instance.test.ts b/packages/@aws-cdk/aws-ec2/test/instance.test.ts index 7f40cd8f202a0..3c945a84d342b 100644 --- a/packages/@aws-cdk/aws-ec2/test/instance.test.ts +++ b/packages/@aws-cdk/aws-ec2/test/instance.test.ts @@ -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, @@ -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: { @@ -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', () => { @@ -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(), @@ -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'); }); });