Skip to content

Commit bbde879

Browse files
authored
chore(ebs): set default volumeType to gp3(under feature flag) (#29934)
### Issue # (if applicable) As the EBS console is now having `gp3` as the default volumeType, this PR set the default volumeType to gp3 if undefined under feature flag. Closes #29931 ### Reason for this change ### Description of changes ### Description of how you validated changes I have deployed the sample below and verified the volume type is `gp3` from console. ```ts import { Stack, App, Size, aws_ec2 as ec2 } from 'aws-cdk-lib'; import * as cxapi from 'aws-cdk-lib/cx-api'; const app = new App(); const stack = new Stack(app, 'demo-stack'); stack.node.setContext(cxapi.EBS_DEFAULT_GP3, true); // should create a gp3 volume new ec2.Volume(stack, 'Volume', { availabilityZone: 'us-east-1a', size: Size.gibibytes(500), }); ``` ### 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 5f8e52e commit bbde879

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

packages/aws-cdk-lib/aws-ec2/lib/volume.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { CfnVolume } from './ec2.generated';
33
import { IInstance } from './instance';
44
import { AccountRootPrincipal, Grant, IGrantable } from '../../aws-iam';
55
import { IKey, ViaServicePrincipal } from '../../aws-kms';
6-
import { IResource, Resource, Size, SizeRoundingBehavior, Stack, Token, Tags, Names, RemovalPolicy } from '../../core';
6+
import { IResource, Resource, Size, SizeRoundingBehavior, Stack, Token, Tags, Names, RemovalPolicy, FeatureFlags } from '../../core';
77
import { md5hash } from '../../core/lib/helpers-internal';
8+
import * as cxapi from '../../cx-api';
89

910
/**
1011
* Block device
@@ -65,7 +66,8 @@ export interface EbsDeviceOptionsBase {
6566
* The EBS volume type
6667
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html
6768
*
68-
* @default `EbsDeviceVolumeType.GP2`
69+
* @default `EbsDeviceVolumeType.GENERAL_PURPOSE_SSD` or `EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3` if
70+
* `@aws-cdk/aws-ec2:ebsDefaultGp3Volume` is enabled.
6971
*/
7072
readonly volumeType?: EbsDeviceVolumeType;
7173
}
@@ -621,7 +623,9 @@ export class Volume extends VolumeBase {
621623
size: props.size?.toGibibytes({ rounding: SizeRoundingBehavior.FAIL }),
622624
snapshotId: props.snapshotId,
623625
throughput: props.throughput,
624-
volumeType: props.volumeType ?? EbsDeviceVolumeType.GENERAL_PURPOSE_SSD,
626+
volumeType: props.volumeType ??
627+
(FeatureFlags.of(this).isEnabled(cxapi.EBS_DEFAULT_GP3) ?
628+
EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3 : EbsDeviceVolumeType.GENERAL_PURPOSE_SSD),
625629
});
626630
resource.applyRemovalPolicy(props.removalPolicy);
627631

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Match, Template } from '../../assertions';
22
import { AccountRootPrincipal, Role } from '../../aws-iam';
33
import * as kms from '../../aws-kms';
44
import * as cdk from '../../core';
5+
import * as cxapi from '../../cx-api';
56
import {
67
AmazonLinuxGeneration,
78
EbsDeviceVolumeType,
@@ -457,6 +458,23 @@ describe('volume', () => {
457458
});
458459
});
459460

461+
test('EBS_DEFAULT_GP3 feature flag', () => {
462+
// GIVEN
463+
const stack = new cdk.Stack();
464+
465+
// WHEN
466+
stack.node.setContext(cxapi.EBS_DEFAULT_GP3, true);
467+
new Volume(stack, 'Volume', {
468+
availabilityZone: 'us-east-1a',
469+
size: cdk.Size.gibibytes(500),
470+
});
471+
472+
// THEN
473+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::Volume', {
474+
VolumeType: 'gp3',
475+
});
476+
});
477+
460478
describe('grantAttachVolume to any instance with encryption', () => {
461479
test('with default key policies', () => {
462480
// GIVEN

packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Flags come in three types:
6767
| [@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse](#aws-cdkaws-codepipelinecrossaccountkeysdefaultvaluetofalse) | Enables Pipeline to set the default value for crossAccountKeys to false. | 2.127.0 | (default) |
6868
| [@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2](#aws-cdkaws-codepipelinedefaultpipelinetypetov2) | Enables Pipeline to set the default pipeline type to V2. | 2.133.0 | (default) |
6969
| [@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope](#aws-cdkaws-kmsreducecrossaccountregionpolicyscope) | When enabled, IAM Policy created from KMS key grant will reduce the resource scope to this key only. | 2.134.0 | (fix) |
70-
| [@aws-cdk/aws-eks:nodegroupNameAttribute](#aws-cdkaws-eksnodegroupnameattribute) | When enabled, nodegroupName attribute of the provisioned EKS NodeGroup will not have the cluster name prefix. | 2.139.0 | (fix) |
70+
| [@aws-cdk/aws-eks:nodegroupNameAttribute](#aws-cdkaws-eksnodegroupnameattribute) | When enabled, nodegroupName attribute of the managed EKS NodeGroup will not have the cluster name prefix. | 2.138.0 | (fix) |
71+
| [@aws-cdk/aws-ec2:ebsDefaultGp3Volume](#aws-cdkaws-ec2ebsdefaultgp3volume) | When enabled, the default volume type of the EBS volume will be GP3 | 2.139.0 | (default) |
7172

7273
<!-- END table -->
7374

@@ -126,7 +127,8 @@ The following json shows the current recommended set of flags, as `cdk init` wou
126127
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
127128
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
128129
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
129-
"@aws-cdk/aws-eks:nodegroupNameAttribute": true
130+
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
131+
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true
130132
}
131133
}
132134
```
@@ -1280,5 +1282,18 @@ any prefix.
12801282
| (not in v1) | | |
12811283
| 2.139.0 | `false` | `true` |
12821284

1285+
### @aws-cdk/aws-ec2:ebsDefaultGp3Volume
1286+
1287+
*When enabled, the default volume type of the EBS volume will be GP3* (default)
1288+
1289+
When this featuer flag is enabled, the default volume type of the EBS volume will be `EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3`.
1290+
1291+
1292+
| Since | Default | Recommended |
1293+
| ----- | ----- | ----- |
1294+
| (not in v1) | | |
1295+
| 2.139.0 | `false` | `true` |
1296+
1297+
**Compatibility with old behavior:** Pass `volumeType: EbsDeviceVolumeType.GENERAL_PURPOSE_SSD` to `Volume` construct to restore the previous behavior.
12831298

12841299
<!-- END details -->

packages/aws-cdk-lib/cx-api/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,19 @@ _cdk.json_
326326
}
327327
}
328328
```
329+
330+
* `@aws-cdk/aws-ec2:ebsDefaultGp3Volume`
331+
332+
When enabled, the default volume type of the EBS volume will be GP3.
333+
334+
When this featuer flag is enabled, the default volume type of the EBS volume will be `EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3`
335+
336+
_cdk.json_
337+
338+
```json
339+
{
340+
"context": {
341+
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true
342+
}
343+
}
344+
```

packages/aws-cdk-lib/cx-api/lib/features.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export const CODEPIPELINE_CROSS_ACCOUNT_KEYS_DEFAULT_VALUE_TO_FALSE = '@aws-cdk/
102102
export const CODEPIPELINE_DEFAULT_PIPELINE_TYPE_TO_V2 = '@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2';
103103
export const KMS_REDUCE_CROSS_ACCOUNT_REGION_POLICY_SCOPE = '@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope';
104104
export const EKS_NODEGROUP_NAME = '@aws-cdk/aws-eks:nodegroupNameAttribute';
105+
export const EBS_DEFAULT_GP3 = '@aws-cdk/aws-ec2:ebsDefaultGp3Volume';
105106

106107
export const FLAGS: Record<string, FlagInfo> = {
107108
//////////////////////////////////////////////////////////////////////
@@ -1047,6 +1048,18 @@ export const FLAGS: Record<string, FlagInfo> = {
10471048
introducedIn: { v2: '2.139.0' },
10481049
recommendedValue: true,
10491050
},
1051+
1052+
//////////////////////////////////////////////////////////////////////
1053+
[EBS_DEFAULT_GP3]: {
1054+
type: FlagType.ApiDefault,
1055+
summary: 'When enabled, the default volume type of the EBS volume will be GP3',
1056+
detailsMd: `
1057+
When this featuer flag is enabled, the default volume type of the EBS volume will be \`EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3\`.
1058+
`,
1059+
introducedIn: { v2: 'V2NEXT' },
1060+
recommendedValue: true,
1061+
compatibilityWithOldBehaviorMd: 'Pass `volumeType: EbsDeviceVolumeType.GENERAL_PURPOSE_SSD` to `Volume` construct to restore the previous behavior.',
1062+
},
10501063
};
10511064

10521065
const CURRENT_MV = 'v2';

0 commit comments

Comments
 (0)