Skip to content

Commit 82ec0ff

Browse files
cohalzrix0rrr
authored andcommitted
feat(aws-ecs): add support Amazon Linux 2 (#1484)
Fixes #1483.
1 parent fefa764 commit 82ec0ff

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/@aws-cdk/aws-ecs/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', {
8888
vpc,
8989
instanceType: new ec2.InstanceType('t2.xlarge'),
9090
machineImage: new EcsOptimizedAmi(),
91+
// Or use ECS-Optimized Amazon Linux 2 AMI
92+
// machineImage: new EcsOptimizedAmi({ generation: ec2.AmazonLinuxGeneration.AmazonLinux2 }),
9193
desiredCapacity: 3,
9294
// ... other options here ...
9395
});

packages/@aws-cdk/aws-ecs/lib/cluster.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,37 @@ export class Cluster extends cdk.Construct implements ICluster {
184184
}
185185
}
186186

187+
export interface EcsOptimizedAmiProps {
188+
/**
189+
* What generation of Amazon Linux to use
190+
*
191+
* @default AmazonLinux
192+
*/
193+
generation?: ec2.AmazonLinuxGeneration;
194+
}
195+
187196
/**
188197
* Construct a Linux machine image from the latest ECS Optimized AMI published in SSM
189198
*/
190-
export class EcsOptimizedAmi implements ec2.IMachineImageSource {
191-
private static AmiParameterName = "/aws/service/ecs/optimized-ami/amazon-linux/recommended";
199+
export class EcsOptimizedAmi implements ec2.IMachineImageSource {
200+
private readonly generation: ec2.AmazonLinuxGeneration;
201+
private readonly amiParameterName: string;
202+
203+
constructor(props?: EcsOptimizedAmiProps) {
204+
this.generation = (props && props.generation) || ec2.AmazonLinuxGeneration.AmazonLinux;
205+
if (this.generation === ec2.AmazonLinuxGeneration.AmazonLinux2) {
206+
this.amiParameterName = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended";
207+
} else {
208+
this.amiParameterName = "/aws/service/ecs/optimized-ami/amazon-linux/recommended";
209+
}
210+
}
192211

193212
/**
194213
* Return the correct image
195214
*/
196215
public getImage(scope: cdk.Construct): ec2.MachineImage {
197216
const ssmProvider = new cdk.SSMParameterProvider(scope, {
198-
parameterName: EcsOptimizedAmi.AmiParameterName
217+
parameterName: this.amiParameterName
199218
});
200219

201220
const json = ssmProvider.parameterValue("{\"image_id\": \"\"}");

0 commit comments

Comments
 (0)