amazon-web-servicesaws-cdkaws-auto-scalinglaunch-configuration

AWS CDK (Typescript) How run AutoScalingGroup without use deprecated launch configurations?


I am using AWS CDK (Typescript version) to create a AutoScalingGroup:

const asg = new autoscaling.AutoScalingGroup(this, 'ASG', {
  instanceType: ec2.InstanceType.of(
    ec2.InstanceClass.T2,
    ec2.InstanceSize.MICRO
  ),
  machineImage: ami,
  minCapacity: 2,
  maxCapacity: 5,
  securityGroup: securityGroup,
  vpc,
  role: role,
});

Everything works and I have 2 instances:

enter image description here

However, I was extremely surprised that CDK uses Launch configurations when it marks this method as deprecated:

enter image description here

How can I solve this problem is using the AutoScalingGroup?

enter image description here

If possible, can you give me an example code?

Best Regards


Solution

  • The AutoScalingGroup construct accepts to pass the launchTemplate parameter.

    https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling.AutoScalingGroup.html#launchtemplate

    You need to pass there the object of the ILaunchTemplate interface described here: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.ILaunchTemplate.html

    Without creating a launch template explicitly, you'll always use the old launch configurations. My guess is that AWS doesn't make new templates default for the backward compatibility of CDK.