Skip to content

Commit

Permalink
feat(ecs): add Amazon Linux 2023 to EcsOptimizedImage (#26989)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
layertwo authored Sep 9, 2023
1 parent 7161005 commit 0bac8a5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/lib/amis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/' : '')
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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/' : '')
Expand Down
29 changes: 29 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0bac8a5

Please sign in to comment.