Search code examples
amazon-web-servicesamazon-ec2aws-cdkamazon-ami

How do I specify an ECS-optimized AMI in CDK?


I'm attempting to specify an EC2 launch template using CDK.

So far my template declaration looks like this:

let launch_template = new ec2.LaunchTemplate(
  this, 
  'MyLaunchTemplate', 
  {
    instanceType: params.instanceType,
    machineImage: ec2.MachineImage.latestAmazonLinux(),
  }
);

But the MachineImage.latestAmazonLinux method does not appear to give an ECS-optimized AMI.

I will need to create two launch templates: one which is ECS-optimized, and another which is ECS-optimized with GPU support.

The AMI's I am currently using are:

  • ami-00eb0dc604a8124fd
  • ami-03d0d75de9d82f509

Is there any way in CDK to specify that an ECS optimized AMI is required, and that an AMI with GPU support is required?

And if not, how can I specify a specific AMI in CDK when creating a launch template?


Solution

  • You can use the EcsOptimizedImage construct ecs.EcsOptimizedImage.amazonLinux2(ecs.AmiHardwareType.GPU)

    let launch_template = new ec2.LaunchTemplate(
      this, 
      'MyLaunchTemplate', 
      {
        instanceType: params.instanceType,
        machineImage: ecs.EcsOptimizedImage.amazonLinux2(ecs.AmiHardwareType.GPU)
      }
    );
    

    Also in order to pick the exact AMI you can give the region ami map

     machineImage: ec2.MachineImage.genericLinux({
              'us-east-1': 'ami-xxxxxxxxxx',
            })