Add support for Launch Templates
Currently the CDK configures EC2 autoscaling using LaunchConfigurations, we should add support for Launch Templates which enables more advanced features such as:
- Mixed auto scaling groups
- Versioning
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
Hello. I was wondering if there is an ETA of this ticket. It would be very helpful if it were implemented for my team.
Are we going to support AWS::EC2::LaunchTemplate first? Is there any PR working on this?
Also relevant (some LT & ASG design discussion going on) : https://github.com/aws/aws-cdk/issues/1403
https://github.com/aws/aws-cdk/issues/1403#issuecomment-603931579
I would also appreciate the availability of a plain LaunchTemplate Construct instead of the low level CFNLaunchTemplate Construct.
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?
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?
The issue is still open, we don't have an ETA to when it will be added.
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...
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
};
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,
},
],
},
};
Thanks for the Launch Template support! Any advance in the AutoScalingGroup and Launch Template configuration?
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.
Is there any ETA for MixedInstancesPolicy?
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/
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.
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 ?
Another few months have gone by... Any updates on this?
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?