From 43013d04f3a2637b799b9a57916751c3d04b9a2f Mon Sep 17 00:00:00 2001 From: Arun Annamalai <36392755+arun-annamalai@users.noreply.github.com> Date: Wed, 12 Jul 2023 09:16:16 -0400 Subject: [PATCH] fix(ecs): Windows ECS Optimized AMI SSM parameter format is incorrect (#26326) The format of the Windows ECS optimized ssm parameter used by CDK is incorrect. The correct format can be found [here](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/retrieve-ecs-optimized_windows_AMI.html) ``` Current incorrect format: /aws/service/ecs/optimized-ami/windows_server/${this.windowsVersion}/english/full/recommended/image_id Correct format: /aws/service/ami-windows-latest/Windows_Server-${this.windowsVersion}-English-Full-ECS_Optimized ``` Closes #26327. ---- *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 +++++++++------ packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts | 10 +++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/aws-cdk-lib/aws-ecs/lib/amis.ts b/packages/aws-cdk-lib/aws-ecs/lib/amis.ts index 7069cf43644c5..9d68b2dc7fce9 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/amis.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/amis.ts @@ -31,6 +31,7 @@ export enum AmiHardwareType { * ECS-optimized Windows version list */ export enum WindowsOptimizedVersion { + SERVER_2022 = '2022', SERVER_2019 = '2019', SERVER_2016 = '2016', } @@ -134,13 +135,14 @@ export class EcsOptimizedAmi implements ec2.IMachineImage { } // set the SSM parameter name - this.amiParameterName = '/aws/service/ecs/optimized-ami/' + this.amiParameterName = '/aws/service/' + + (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.windowsVersion ? `windows_server/${this.windowsVersion}/english/full/` : '') + + (this.windowsVersion ? `Windows_Server-${this.windowsVersion}-English-Full-ECS_Optimized/` : '') + (this.hwType === AmiHardwareType.GPU ? 'gpu/' : '') + (this.hwType === AmiHardwareType.ARM ? 'arm64/' : '') - + 'recommended/image_id'; + + (this.windowsVersion ? 'image_id' : 'recommended/image_id'); this.cachedInContext = props?.cachedInContext ?? false; } @@ -247,13 +249,14 @@ export class EcsOptimizedImage implements ec2.IMachineImage { } // set the SSM parameter name - this.amiParameterName = '/aws/service/ecs/optimized-ami/' + this.amiParameterName = '/aws/service/' + + (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.windowsVersion ? `windows_server/${this.windowsVersion}/english/full/` : '') + + (this.windowsVersion ? `Windows_Server-${this.windowsVersion}-English-Full-ECS_Optimized/` : '') + (this.hwType === AmiHardwareType.GPU ? 'gpu/' : '') + (this.hwType === AmiHardwareType.ARM ? 'arm64/' : '') - + 'recommended/image_id'; + + (this.windowsVersion ? 'image_id' : 'recommended/image_id'); this.cachedInContext = props?.cachedInContext ?? false; } 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 60af60677adfb..a05083dcc0a5c 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts @@ -792,7 +792,7 @@ describe('cluster', () => { // THEN Template.fromStack(stack).hasResourceProperties('AWS::AutoScaling::LaunchConfiguration', { ImageId: { - Ref: 'SsmParameterValueawsserviceecsoptimizedamiwindowsserver2019englishfullrecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter', + Ref: 'SsmParameterValueawsserviceamiwindowslatestWindowsServer2019EnglishFullECSOptimizedimageidC96584B6F00A464EAD1953AFF4B05118Parameter', }, InstanceType: 't2.micro', IamInstanceProfile: { @@ -901,9 +901,9 @@ describe('cluster', () => { const assembly = app.synth(); const template = assembly.getStackByName(stack.stackName).template; expect(template.Parameters).toEqual({ - SsmParameterValueawsserviceecsoptimizedamiwindowsserver2019englishfullrecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter: { + SsmParameterValueawsserviceamiwindowslatestWindowsServer2019EnglishFullECSOptimizedimageidC96584B6F00A464EAD1953AFF4B05118Parameter: { Type: 'AWS::SSM::Parameter::Value', - Default: '/aws/service/ecs/optimized-ami/windows_server/2019/english/full/recommended/image_id', + Default: '/aws/service/ami-windows-latest/Windows_Server-2019-English-Full-ECS_Optimized/image_id', }, }); @@ -1135,9 +1135,9 @@ describe('cluster', () => { const assembly = app.synth(); const template = assembly.getStackByName(stack.stackName).template; expect(template.Parameters).toEqual({ - SsmParameterValueawsserviceecsoptimizedamiwindowsserver2019englishfullrecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter: { + SsmParameterValueawsserviceamiwindowslatestWindowsServer2019EnglishFullECSOptimizedimageidC96584B6F00A464EAD1953AFF4B05118Parameter: { Type: 'AWS::SSM::Parameter::Value', - Default: '/aws/service/ecs/optimized-ami/windows_server/2019/english/full/recommended/image_id', + Default: '/aws/service/ami-windows-latest/Windows_Server-2019-English-Full-ECS_Optimized/image_id', }, });