Skip to content

Commit

Permalink
fix(ecs): Correct Windows ECS Optimized AMI SSM parameter
Browse files Browse the repository at this point in the history
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)

Closes #26327.
  • Loading branch information
arun-annamalai committed Jul 11, 2023
1 parent 73f2741 commit 7d88e3f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 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/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/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
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ describe('cluster', () => {
expect(template.Parameters).toEqual({
SsmParameterValueawsserviceecsoptimizedamiwindowsserver2019englishfullrecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter: {
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 @@ -1137,7 +1137,7 @@ describe('cluster', () => {
expect(template.Parameters).toEqual({
SsmParameterValueawsserviceecsoptimizedamiwindowsserver2019englishfullrecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter: {
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 7d88e3f

Please sign in to comment.