aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.65k stars 3.91k forks source link

(aws-ecs): Use the proper AMI when using a Graviton2 instance #14433

Closed darko-mesaros closed 3 years ago

darko-mesaros commented 3 years ago

Okay, so, I wish to launch a Gravion2 ECS Cluster, and I wish to use CDK for that. I am using .addCapacity to create the AutoScaling Group for my cluster, and I am only specifying the instance type.

Unless I define a machineImage property and use the ECS optimized AMI (for aarch64), it will attempt to launch it with the regular x86 ECS optimized AMI. And the launch of EC2 Instances fail:

import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as ecs from '@aws-cdk/aws-ecs';

export class ClusterStack extends cdk.Stack {
  public readonly vpc: ec2.Vpc;
  public readonly cluster: ecs.Cluster;

  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    this.vpc = new ec2.Vpc(this, 'VPC', { maxAzs: 2});
    this.cluster = new ecs.Cluster(this, 'Cluster', {vpc: this.vpc});
    this.cluster.addCapacity('DefaultCapacity', {
      instanceType: new ec2.InstanceType('c6g.large'),
    });
  }
}

Use Case

We can keep using the dynamic nature of AMI IDs and not have to hardcode the AMI id for the ECS optimized image for Graviton2.

Proposed Solution

Ideally if CDK could verify if the instance type is an ARM based instance, it would use the appropriate AMI.

Other

CDK Version: 1.100.0 (build d996c6d) OS: Mac Os Big Sur Kernel: Darwin m1-air.lan 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:06:51 PST 2021; root:xnu-7195.81.3~1/RELEASE_ARM64_T8101 arm64


This is a :rocket: Feature Request

SoManyHs commented 3 years ago

Hi @darko-mesaros!

The missing piece here is you also need to specify machineImage as a property when you call addCapacity. I've updated the README with a working example in https://github.com/aws/aws-cdk/pull/14536 -- hope that helps!

darko-mesaros commented 3 years ago

Ah good! I was under the impression that I would need to know the AMI ID to be used at all times. This is a much better option! Thanks! 👏

github-actions[bot] commented 3 years ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.