Skip to content

Commit

Permalink
chore(ebs): set default volumeType to gp3(under feature flag) (#29934)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
pahud committed Apr 30, 2024
1 parent 5f8e52e commit bbde879
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
10 changes: 7 additions & 3 deletions packages/aws-cdk-lib/aws-ec2/lib/volume.ts
Expand Up @@ -3,8 +3,9 @@ import { CfnVolume } from './ec2.generated';
import { IInstance } from './instance';
import { AccountRootPrincipal, Grant, IGrantable } from '../../aws-iam';
import { IKey, ViaServicePrincipal } from '../../aws-kms';
import { IResource, Resource, Size, SizeRoundingBehavior, Stack, Token, Tags, Names, RemovalPolicy } from '../../core';
import { IResource, Resource, Size, SizeRoundingBehavior, Stack, Token, Tags, Names, RemovalPolicy, FeatureFlags } from '../../core';
import { md5hash } from '../../core/lib/helpers-internal';
import * as cxapi from '../../cx-api';

/**
* Block device
Expand Down Expand Up @@ -65,7 +66,8 @@ export interface EbsDeviceOptionsBase {
* The EBS volume type
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html
*
* @default `EbsDeviceVolumeType.GP2`
* @default `EbsDeviceVolumeType.GENERAL_PURPOSE_SSD` or `EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3` if
* `@aws-cdk/aws-ec2:ebsDefaultGp3Volume` is enabled.
*/
readonly volumeType?: EbsDeviceVolumeType;
}
Expand Down Expand Up @@ -621,7 +623,9 @@ export class Volume extends VolumeBase {
size: props.size?.toGibibytes({ rounding: SizeRoundingBehavior.FAIL }),
snapshotId: props.snapshotId,
throughput: props.throughput,
volumeType: props.volumeType ?? EbsDeviceVolumeType.GENERAL_PURPOSE_SSD,
volumeType: props.volumeType ??
(FeatureFlags.of(this).isEnabled(cxapi.EBS_DEFAULT_GP3) ?
EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3 : EbsDeviceVolumeType.GENERAL_PURPOSE_SSD),
});
resource.applyRemovalPolicy(props.removalPolicy);

Expand Down
18 changes: 18 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/test/volume.test.ts
Expand Up @@ -2,6 +2,7 @@ import { Match, Template } from '../../assertions';
import { AccountRootPrincipal, Role } from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as cdk from '../../core';
import * as cxapi from '../../cx-api';
import {
AmazonLinuxGeneration,
EbsDeviceVolumeType,
Expand Down Expand Up @@ -457,6 +458,23 @@ describe('volume', () => {
});
});

test('EBS_DEFAULT_GP3 feature flag', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
stack.node.setContext(cxapi.EBS_DEFAULT_GP3, true);
new Volume(stack, 'Volume', {
availabilityZone: 'us-east-1a',
size: cdk.Size.gibibytes(500),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::EC2::Volume', {
VolumeType: 'gp3',
});
});

describe('grantAttachVolume to any instance with encryption', () => {
test('with default key policies', () => {
// GIVEN
Expand Down
19 changes: 17 additions & 2 deletions packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md
Expand Up @@ -67,7 +67,8 @@ Flags come in three types:
| [@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse](#aws-cdkaws-codepipelinecrossaccountkeysdefaultvaluetofalse) | Enables Pipeline to set the default value for crossAccountKeys to false. | 2.127.0 | (default) |
| [@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2](#aws-cdkaws-codepipelinedefaultpipelinetypetov2) | Enables Pipeline to set the default pipeline type to V2. | 2.133.0 | (default) |
| [@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) |
| [@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) |
| [@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) |
| [@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) |

<!-- END table -->

Expand Down Expand Up @@ -126,7 +127,8 @@ The following json shows the current recommended set of flags, as `cdk init` wou
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
"@aws-cdk/aws-eks:nodegroupNameAttribute": true
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true
}
}
```
Expand Down Expand Up @@ -1280,5 +1282,18 @@ any prefix.
| (not in v1) | | |
| 2.139.0 | `false` | `true` |

### @aws-cdk/aws-ec2:ebsDefaultGp3Volume

*When enabled, the default volume type of the EBS volume will be GP3* (default)

When this featuer flag is enabled, the default volume type of the EBS volume will be `EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3`.


| Since | Default | Recommended |
| ----- | ----- | ----- |
| (not in v1) | | |
| 2.139.0 | `false` | `true` |

**Compatibility with old behavior:** Pass `volumeType: EbsDeviceVolumeType.GENERAL_PURPOSE_SSD` to `Volume` construct to restore the previous behavior.

<!-- END details -->
16 changes: 16 additions & 0 deletions packages/aws-cdk-lib/cx-api/README.md
Expand Up @@ -326,3 +326,19 @@ _cdk.json_
}
}
```

* `@aws-cdk/aws-ec2:ebsDefaultGp3Volume`

When enabled, the default volume type of the EBS volume will be GP3.

When this featuer flag is enabled, the default volume type of the EBS volume will be `EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3`

_cdk.json_

```json
{
"context": {
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true
}
}
```
13 changes: 13 additions & 0 deletions packages/aws-cdk-lib/cx-api/lib/features.ts
Expand Up @@ -102,6 +102,7 @@ export const CODEPIPELINE_CROSS_ACCOUNT_KEYS_DEFAULT_VALUE_TO_FALSE = '@aws-cdk/
export const CODEPIPELINE_DEFAULT_PIPELINE_TYPE_TO_V2 = '@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2';
export const KMS_REDUCE_CROSS_ACCOUNT_REGION_POLICY_SCOPE = '@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope';
export const EKS_NODEGROUP_NAME = '@aws-cdk/aws-eks:nodegroupNameAttribute';
export const EBS_DEFAULT_GP3 = '@aws-cdk/aws-ec2:ebsDefaultGp3Volume';

export const FLAGS: Record<string, FlagInfo> = {
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1047,6 +1048,18 @@ export const FLAGS: Record<string, FlagInfo> = {
introducedIn: { v2: '2.139.0' },
recommendedValue: true,
},

//////////////////////////////////////////////////////////////////////
[EBS_DEFAULT_GP3]: {
type: FlagType.ApiDefault,
summary: 'When enabled, the default volume type of the EBS volume will be GP3',
detailsMd: `
When this featuer flag is enabled, the default volume type of the EBS volume will be \`EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3\`.
`,
introducedIn: { v2: 'V2NEXT' },
recommendedValue: true,
compatibilityWithOldBehaviorMd: 'Pass `volumeType: EbsDeviceVolumeType.GENERAL_PURPOSE_SSD` to `Volume` construct to restore the previous behavior.',
},
};

const CURRENT_MV = 'v2';
Expand Down

0 comments on commit bbde879

Please sign in to comment.