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): support throughput on LaunchTemplate EBS volumes #30317

Closed
wants to merge 1 commit into from
Closed
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"BlockDeviceMappings": [
{
"DeviceName": "/dev/xvda",
"Ebs": {
"Throughput": 250,
"VolumeSize": 15,
"VolumeType": "gp3"
}
}
],
"MetadataOptions": {
"HttpEndpoint": "enabled",
"HttpProtocolIpv6": "enabled",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const lt = new ec2.LaunchTemplate(stack, 'LT', {
httpTokens: ec2.LaunchTemplateHttpTokens.REQUIRED,
instanceMetadataTags: true,
securityGroup: sg1,
blockDevices: [{
deviceName: '/dev/xvda',
volume: ec2.BlockDeviceVolume.ebs(15, {
volumeType: ec2.EbsDeviceVolumeType.GP3,
throughput: 250,
}),
}],
});

const sg2 = new ec2.SecurityGroup(stack, 'sg2', {
Expand Down
26 changes: 25 additions & 1 deletion packages/aws-cdk-lib/aws-ec2/lib/private/ebs-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,30 @@ function synthesizeBlockDeviceMappings<RT, NDT>(construct: Construct, blockDevic

if (ebs) {

const { iops, volumeType, kmsKey, ...rest } = ebs;
const { iops, throughput, volumeType, kmsKey, ...rest } = ebs;

if (throughput) {
const throughputRange = { Min: 125, Max: 1000 };
const { Min, Max } = throughputRange;

if (volumeType != EbsDeviceVolumeType.GP3) {
throw new Error('throughput property requires volumeType: EbsDeviceVolumeType.GP3');
}

if (throughput < Min || throughput > Max) {
throw new Error(
`throughput property takes a minimum of ${Min} and a maximum of ${Max}`,
);
}

const maximumThroughputRatio = 0.25;
if (iops) {
const iopsRatio = (throughput / iops);
if (iopsRatio > maximumThroughputRatio) {
throw new Error(`Throughput (MiBps) to iops ratio of ${iopsRatio} is too high; maximum is ${maximumThroughputRatio} MiBps per iops`);
}
}
}

if (!iops) {
if (volumeType === EbsDeviceVolumeType.IO1 || volumeType === EbsDeviceVolumeType.IO2) {
Expand All @@ -43,6 +66,7 @@ function synthesizeBlockDeviceMappings<RT, NDT>(construct: Construct, blockDevic
finalEbs = {
...rest,
iops,
throughput,
volumeType,
kmsKeyId: kmsKey?.keyArn,
};
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/lib/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ export interface EbsDeviceOptionsBase {
* `@aws-cdk/aws-ec2:ebsDefaultGp3Volume` is enabled.
*/
readonly volumeType?: EbsDeviceVolumeType;

/**
* The throughput that the volume supports, in MiB/s
* Takes a minimum of 125 and maximum of 1000.
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html
* @default - 125 MiB/s. Only valid on gp3 volumes.
*/
readonly throughput?: number;
}

/**
Expand Down
56 changes: 56 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/test/launch-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ describe('LaunchTemplate', () => {
}, {
deviceName: 'ephemeral',
volume: BlockDeviceVolume.ephemeral(0),
}, {
deviceName: 'gp3-with-throughput',
volume: BlockDeviceVolume.ebs(15, {
volumeType: EbsDeviceVolumeType.GP3,
throughput: 350,
}),
},
];

Expand Down Expand Up @@ -366,10 +372,60 @@ describe('LaunchTemplate', () => {
DeviceName: 'ephemeral',
VirtualName: 'ephemeral0',
},
{
DeviceName: 'gp3-with-throughput',
Ebs: {
VolumeSize: 15,
VolumeType: 'gp3',
Throughput: 350,
},
},
],
},
});
});
test.each([124, 1001])('throws if throughput is set less than 125 or more than 1000', (throughput) => {
expect(() => {
new LaunchTemplate(stack, 'LaunchTemplate', {
blockDevices: [{
deviceName: 'ebs',
volume: BlockDeviceVolume.ebs(15, {
volumeType: EbsDeviceVolumeType.GP3,
throughput,
}),
}],
});
}).toThrow(/throughput property takes a minimum of 125 and a maximum of 1000/);
});
test.each([
...Object.values(EbsDeviceVolumeType).filter((v) => v !== 'gp3'),
])('throws if throughput is set on any volume type other than GP3', (volumeType) => {
expect(() => {
new LaunchTemplate(stack, 'LaunchTemplate', {
blockDevices: [{
deviceName: 'ebs',
volume: BlockDeviceVolume.ebs(15, {
volumeType: volumeType,
throughput: 150,
}),
}],
});
}).toThrow(/throughput property requires volumeType: EbsDeviceVolumeType.GP3/);
});
test('throws if throughput / iops ratio is greater than 0.25', () => {
expect(() => {
new LaunchTemplate(stack, 'LaunchTemplate', {
blockDevices: [{
deviceName: 'ebs',
volume: BlockDeviceVolume.ebs(15, {
volumeType: EbsDeviceVolumeType.GP3,
throughput: 751,
iops: 3000,
}),
}],
});
}).toThrow('Throughput (MiBps) to iops ratio of 0.25033333333333335 is too high; maximum is 0.25 MiBps per iops');
});

test('Given instance profile', () => {
// GIVEN
Expand Down
Loading