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();