Skip to content

Commit

Permalink
feat(aws-ecs): add support Amazon Linux 2 (aws#1484)
Browse files Browse the repository at this point in the history
Fixes aws#1483.
  • Loading branch information
cohalz authored and rix0rrr committed Jan 6, 2019
1 parent fefa764 commit 82ec0ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', {
vpc,
instanceType: new ec2.InstanceType('t2.xlarge'),
machineImage: new EcsOptimizedAmi(),
// Or use ECS-Optimized Amazon Linux 2 AMI
// machineImage: new EcsOptimizedAmi({ generation: ec2.AmazonLinuxGeneration.AmazonLinux2 }),
desiredCapacity: 3,
// ... other options here ...
});
Expand Down
25 changes: 22 additions & 3 deletions packages/@aws-cdk/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,37 @@ export class Cluster extends cdk.Construct implements ICluster {
}
}

export interface EcsOptimizedAmiProps {
/**
* What generation of Amazon Linux to use
*
* @default AmazonLinux
*/
generation?: ec2.AmazonLinuxGeneration;
}

/**
* Construct a Linux machine image from the latest ECS Optimized AMI published in SSM
*/
export class EcsOptimizedAmi implements ec2.IMachineImageSource {
private static AmiParameterName = "/aws/service/ecs/optimized-ami/amazon-linux/recommended";
export class EcsOptimizedAmi implements ec2.IMachineImageSource {
private readonly generation: ec2.AmazonLinuxGeneration;
private readonly amiParameterName: string;

constructor(props?: EcsOptimizedAmiProps) {
this.generation = (props && props.generation) || ec2.AmazonLinuxGeneration.AmazonLinux;
if (this.generation === ec2.AmazonLinuxGeneration.AmazonLinux2) {
this.amiParameterName = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended";
} else {
this.amiParameterName = "/aws/service/ecs/optimized-ami/amazon-linux/recommended";
}
}

/**
* Return the correct image
*/
public getImage(scope: cdk.Construct): ec2.MachineImage {
const ssmProvider = new cdk.SSMParameterProvider(scope, {
parameterName: EcsOptimizedAmi.AmiParameterName
parameterName: this.amiParameterName
});

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

0 comments on commit 82ec0ff

Please sign in to comment.