From 9bb7b111e925ce211d0b5fbac5fd8ae09547494e Mon Sep 17 00:00:00 2001 From: Lucas Messenger <1335960+layertwo@users.noreply.github.com> Date: Sat, 9 Sep 2023 02:52:55 -0700 Subject: [PATCH] feat(ecs): add Amazon Linux 2023 to EcsOptimizedImage (#26989) Add support for selecting Amazon Linux 2023 ECS Optimized images. Reference: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/retrieve-ecs-optimized_AMI.html Closes #26988. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk-lib/aws-ecs/lib/amis.ts | 15 ++++++++++ .../aws-cdk-lib/aws-ecs/test/cluster.test.ts | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/aws-cdk-lib/aws-ecs/lib/amis.ts b/packages/aws-cdk-lib/aws-ecs/lib/amis.ts index 9d68b2dc7fce9..52fcc44f45210 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/amis.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/amis.ts @@ -139,6 +139,7 @@ export class EcsOptimizedAmi implements ec2.IMachineImage { + (this.windowsVersion ? 'ami-windows-latest/' : 'ecs/optimized-ami/') + (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX ? 'amazon-linux/' : '') + (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 ? 'amazon-linux-2/' : '') + + (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2023 ? 'amazon-linux-2023/' : '') + (this.windowsVersion ? `Windows_Server-${this.windowsVersion}-English-Full-ECS_Optimized/` : '') + (this.hwType === AmiHardwareType.GPU ? 'gpu/' : '') + (this.hwType === AmiHardwareType.ARM ? 'arm64/' : '') @@ -192,6 +193,19 @@ export interface EcsOptimizedImageOptions { * Construct a Linux or Windows machine image from the latest ECS Optimized AMI published in SSM */ export class EcsOptimizedImage implements ec2.IMachineImage { + /** + * Construct an Amazon Linux 2023 image from the latest ECS Optimized AMI published in SSM + * + * @param hardwareType ECS-optimized AMI variant to use + */ + public static amazonLinux2023(hardwareType = AmiHardwareType.STANDARD, options: EcsOptimizedImageOptions = {}): EcsOptimizedImage { + return new EcsOptimizedImage({ + generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2023, + hardwareType, + cachedInContext: options.cachedInContext, + }); + } + /** * Construct an Amazon Linux 2 image from the latest ECS Optimized AMI published in SSM * @@ -253,6 +267,7 @@ export class EcsOptimizedImage implements ec2.IMachineImage { + (this.windowsVersion ? 'ami-windows-latest/' : 'ecs/optimized-ami/') + (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX ? 'amazon-linux/' : '') + (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 ? 'amazon-linux-2/' : '') + + (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2023 ? 'amazon-linux-2023/' : '') + (this.windowsVersion ? `Windows_Server-${this.windowsVersion}-English-Full-ECS_Optimized/` : '') + (this.hwType === AmiHardwareType.GPU ? 'gpu/' : '') + (this.hwType === AmiHardwareType.ARM ? 'arm64/' : '') diff --git a/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts b/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts index a05083dcc0a5c..258696e2bfc55 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts @@ -982,6 +982,17 @@ describe('cluster', () => { }); + testDeprecated('allows returning the correct image for linux 2023 for EcsOptimizedAmi', () => { + // GIVEN + const stack = new cdk.Stack(); + const ami = new ecs.EcsOptimizedAmi({ + generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2023, + }); + + expect(ami.getImage(stack).osType).toEqual(ec2.OperatingSystemType.LINUX); + + }); + test('allows returning the correct image for linux for EcsOptimizedImage', () => { // GIVEN const stack = new cdk.Stack(); @@ -1009,6 +1020,24 @@ describe('cluster', () => { }); + test('allows returning the correct image for linux 2023 for EcsOptimizedImage', () => { + // GIVEN + const stack = new cdk.Stack(); + + expect(ecs.EcsOptimizedImage.amazonLinux2023().getImage(stack).osType).toEqual( + ec2.OperatingSystemType.LINUX); + + }); + + test('allows returning the correct image for linux 2023 for EcsOptimizedImage with ARM hardware', () => { + // GIVEN + const stack = new cdk.Stack(); + + expect(ecs.EcsOptimizedImage.amazonLinux2023(ecs.AmiHardwareType.ARM).getImage(stack).osType).toEqual( + ec2.OperatingSystemType.LINUX); + + }); + test('allows returning the correct image for windows for EcsOptimizedImage', () => { // GIVEN const stack = new cdk.Stack();