Skip to content

Commit

Permalink
fix(ecs): Windows ECS Optimized AMI SSM parameter format is incorrect (
Browse files Browse the repository at this point in the history
…#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*
  • Loading branch information
arun-annamalai authored Jul 12, 2023
1 parent 546456a commit 43013d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
15 changes: 9 additions & 6 deletions packages/aws-cdk-lib/aws-ecs/lib/amis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum AmiHardwareType {
* ECS-optimized Windows version list
*/
export enum WindowsOptimizedVersion {
SERVER_2022 = '2022',
SERVER_2019 = '2019',
SERVER_2016 = '2016',
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ describe('cluster', () => {
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AutoScaling::LaunchConfiguration', {
ImageId: {
Ref: 'SsmParameterValueawsserviceecsoptimizedamiwindowsserver2019englishfullrecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter',
Ref: 'SsmParameterValueawsserviceamiwindowslatestWindowsServer2019EnglishFullECSOptimizedimageidC96584B6F00A464EAD1953AFF4B05118Parameter',
},
InstanceType: 't2.micro',
IamInstanceProfile: {
Expand Down Expand Up @@ -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<AWS::EC2::Image::Id>',
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',
},
});

Expand Down Expand Up @@ -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<AWS::EC2::Image::Id>',
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',
},
});

Expand Down

0 comments on commit 43013d0

Please sign in to comment.