Updating a LaunchTemplate and making it the default version with AWS CDK

Viewed 2215

I'm creating and updating a LaunchTemplate using AWS CDK, like

new ec2.LaunchTemplate(stack, "my-template", {
  launchTemplateName: "my-template",
  instanceType: new ec2.InstanceType("g3.4xlarge"),
  machineImage: ec2.MachineImage.lookup({ 
    name: "my-ami-*", 
    owners: [ACCOUNT_ID] 
  }),
  keyName: "my-key",
  role: myRole,
  securityGroup: mySecGroup
})

This creates a launch template as expected, but when I make changes, it results to a new version of the same template and the old version stays as the default. The intention is to update template default version so that when I launch instances using AWS SDK I get up to date instances.

So, how do I do this using AWS CDK?

2 Answers

This is not a solution, but a workaround. You don't need to update default version in the Launch template, you can simply specify in the Autoscaling group to use the latest template version.

From AWS docs: For Launch template version, choose whether the Auto Scaling group uses the default, the latest, or a specific version of the launch template when scaling out.

https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-asg-launch-template.html

Related