aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Add support for Launch Templates

Open NetaNir opened this issue 6 years ago • 25 comments

Currently the CDK configures EC2 autoscaling using LaunchConfigurations, we should add support for Launch Templates which enables more advanced features such as:

NetaNir avatar Mar 15 '20 20:03 NetaNir

Related issues:

  • https://github.com/aws/aws-cdk/issues/3077
  • https://github.com/aws/aws-cdk/issues/6714
  • https://github.com/aws/aws-cdk/pull/6290

NetaNir avatar Mar 15 '20 20:03 NetaNir

Hello. I was wondering if there is an ETA of this ticket. It would be very helpful if it were implemented for my team.

MajaScherman avatar Mar 31 '20 06:03 MajaScherman

Are we going to support AWS::EC2::LaunchTemplate first? Is there any PR working on this?

pahud avatar Apr 03 '20 13:04 pahud

Also relevant (some LT & ASG design discussion going on) : https://github.com/aws/aws-cdk/issues/1403

ddneilson avatar Apr 03 '20 13:04 ddneilson

https://github.com/aws/aws-cdk/issues/1403#issuecomment-603931579

rix0rrr avatar Apr 28 '20 16:04 rix0rrr

I would also appreciate the availability of a plain LaunchTemplate Construct instead of the low level CFNLaunchTemplate Construct.

bitfoo1 avatar Jul 16 '20 08:07 bitfoo1

As others have commented, support for launch templates is something my team would like to see implemented soon. Is there any ETA on when this might be fixed?

chrispernicano avatar Oct 16 '20 14:10 chrispernicano

It seems that a bunch of other tickets mentioning this issue got closed with this issue as favored one but there are no updates here. Any ETA on the topic?

moby04 avatar Jan 01 '21 08:01 moby04

The issue is still open, we don't have an ETA to when it will be added.

NetaNir avatar Jan 01 '21 19:01 NetaNir

anyone has a workaround to overcome this issue before it gets implemented (without using lc)? I got stuck with alb-listener-tg-asg-lt relationship.

first line in https://docs.aws.amazon.com/autoscaling/ec2/userguide/LaunchConfiguration.html says "We strongly recommend that you do not use launch configurations." but if this ticket does not get priorities then I have to give up on cdk, not to mention this is first project i am using cdk.

and this ticket is going to have its first birthday cake very soon...

mingteoh avatar Feb 06 '21 00:02 mingteoh

I was able to create a workaround for this. My specific need was to be able to set gp3 as the volume type and also set the burstable CPU credits to standard. I am using the L2 ECS constructs to create a cluster, then at the very end I create a launch template and replace the existing launch config. Credit to this site for setting me in the right direction: https://ecsworkshop.com/capacity_providers/ec2_spot/

This is in C# but the concept should translate easily


// Not shown: creation of the Cluster and the Auto Scaling Group with minimal instance launch config settings.

// Create launch template with the props we really want
var ec2LaunchTemplate = new LaunchTemplate(this, "ClusterLaunchTemplate", new LaunchTemplateProps
{
    InstanceType = InstanceType.Of(InstanceClass.BURSTABLE3, InstanceSize.MEDIUM),
    CpuCredits = CpuCredits.STANDARD,
    MachineImage = MachineImage.FromSSMParameter(
        "/aws/service/ami-windows-latest/Windows_Server-2004-English-Core-ECS_Optimized/image_id",
        OperatingSystemType.WINDOWS),
    SecurityGroup = ec2SecurityGroup,
    Role = InstanceIamRole, 
    DetailedMonitoring = false,
    BlockDevices = new[]
    {
        new Amazon.CDK.AWS.EC2.BlockDevice()
        {
            DeviceName = "/dev/sda1",
            Volume = Amazon.CDK.AWS.EC2.BlockDeviceVolume.Ebs(
                50,
                new Amazon.CDK.AWS.EC2.EbsDeviceOptions
                {
                    DeleteOnTermination = true,
                    VolumeType = Amazon.CDK.AWS.EC2.EbsDeviceVolumeType.GP3,
                })
        }
    }
});

// Get the underlying CfnAutoScalingGroup from the L2 construct
// CDK escape hatch docs: https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html
var cfnEc2Group = ((CfnAutoScalingGroup)ec2group.Node.DefaultChild);

// Remove the launch config from our stack
ec2group.Node.TryRemoveChild("LaunchConfig");
cfnEc2Group.LaunchConfigurationName = null;

// Attach the launch template to the auto scaling group
CfnLaunchTemplate cfnLaunchTemplate = (CfnLaunchTemplate)ec2LaunchTemplate.Node.DefaultChild;
cfnEc2Group.LaunchTemplate = new CfnAutoScalingGroup.LaunchTemplateSpecificationProperty
{
    LaunchTemplateId = cfnLaunchTemplate.Ref,
    Version = cfnLaunchTemplate.AttrDefaultVersionNumber
};

zstarkebaumcalico avatar Mar 29 '21 20:03 zstarkebaumcalico

thanks @zstarkebaumcalico ! That was super helpful.

Here's a TypeScript example that also uses Spot instances, based on the code above. Hacky but better than nothing until CDK supports launch templates and instanceDistribution.


    // hack until they support LaunchTemplates properly: https://github.com/aws/aws-cdk/issues/6734
    const cfnAsg = asg.node.defaultChild as autoscaling.CfnAutoScalingGroup;
    asg.node.tryRemoveChild('LaunchConfig');
    cfnAsg.launchConfigurationName = undefined;

    const cfnLaunchTemplate = ec2LaunchTemplate.node.defaultChild as ec2.CfnLaunchTemplate;
    cfnAsg.mixedInstancesPolicy = {
        instancesDistribution: {
            onDemandBaseCapacity: 0,
            onDemandPercentageAboveBaseCapacity: 0,
            spotAllocationStrategy: 'capacity-optimized',
        },
        launchTemplate: {
            launchTemplateSpecification: {
                launchTemplateId: cfnLaunchTemplate.ref,
                version: cfnLaunchTemplate.attrLatestVersionNumber,
            },
            // TODO: we could add additional instance types here to possibly save costs further.  If you go into
            // the console and use the wizard to start creating a spot ASG it will recommend similar instance types.
            overrides: [
                {
                    instanceType: instanceType,
                },
            ],
        },
    };

cprice404-aws avatar May 28 '21 19:05 cprice404-aws

Thanks for the Launch Template support! Any advance in the AutoScalingGroup and Launch Template configuration?

Higherings avatar Aug 26 '21 18:08 Higherings

Looking at the workaround hack, I'm trying to instantiate an AutoScalingGroup with only parameters a LaunchTemplate does not contain, and before I can even cast that to a CfnAutoScalingGroup to take on the rest of config from the LaunchTemplate, its constructor complains

Unhandled exception. Amazon.JSII.Runtime.JsiiException: Missing required properties for @aws-cdk/aws-autoscaling.AutoScalingGroupProps: instanceType, machineImage

Guess have to include those redundant parameters.

icelava avatar Sep 16 '21 09:09 icelava

Is there any ETA for MixedInstancesPolicy?

pv93 avatar Oct 12 '21 16:10 pv93

With AWS starting to sunset Launch Configurations, any ETA on when proper support for Launch Templates will come?

https://aws.amazon.com/blogs/compute/amazon-ec2-auto-scaling-will-no-longer-add-support-for-new-ec2-features-to-launch-configurations/

samjarrett avatar Oct 25 '21 23:10 samjarrett

Here is a WIP commit: https://github.com/Martin1994/aws-cdk/commit/07af2e88f6b885a11df93a7716c970ab4c8cd6f1

I will send a PR shortly once I finish its unit tests, but any early feedback is welcomed, especially its design including breaking API changes, as the current ASG construct is deeply coupled with the usage of LC.

I really wonder whether we take the next major version bump (saying CDK 2.0) as a chance to completely rework the ASG construct.

Martin1994 avatar Nov 22 '21 07:11 Martin1994

I'm very eager to use Launch Templates on a new project. @Martin1994's changes look good, but I'm not sure if this would go into version 1 or 2.

Is there any escape hatch that can be used in the meanwhile to achieve this using ASGs ?

neilferreira avatar Nov 29 '21 01:11 neilferreira

Another few months have gone by... Any updates on this?

erikdebruin avatar Feb 09 '22 12:02 erikdebruin

It's been almost 3 years since the initial request for Mixed Instance Auto Scaling groups, and 6 months since a PR has been opened implementing this feature.

It's evident that many people are requesting this feature, so I have to ask: what is the hold up? And is there anything we as the community could do to speed up the process?

pv93 avatar Feb 14 '22 22:02 pv93