Skip to content

Commit 1581af0

Browse files
authored
feat(autoscaling): Auto Scaling Group with Launch Template (#19066)
Add support to launch an Auto Scaling Group from a Launch Template rather than a launch configuration. Closes #6734. ---- High level designs: The current AutoScalingGroup L2 construct, unfortunately, is deeply coupled with LaunchConfiguration. Therefore adding LaunchTemplate to the existing construct is not trivial. There are two different approaches to support LaunchTemplate: 1. Implement another brand new L2 construct with `IAutoScalingGroup` interface. The shared logic between the old and the new one such as adding scaling policies can be extracted to a common parent class. 2. Add LaunchTemplate related interface to the existing L2 construct with minimum breaking changes. Adding a new construct is always guaranteed to be a clean solution code-wise, but it can also cause confusion from the end user about scattered CDK implementation on the same CFN resource, and on the other hand breaks the existing libraries consuming the existing `AutoScalingGroup` construct rather than the `IAutoScalingGroup` interface. Adding LT to the existing construct, as what this PR does, however may change the API behaviour when LT is used. For example, the implementation in this PR turns `AutoScalingGroup.connections` from a public field to a public getter, and will throw an error when the ASG is created from a LT reference, which means existing libraries taking an `AutoScalingGroup` instance as an `IConnectable` will get runtime error in this case. Existing code (i.e. ASG created from the L2 construct with a LC rather than a LT) won't break with this change. This PR picks the latter approach since I believe it provides a better experience overall, mainly because of its continuity. ---- BREAKING CHANGE: Properties relying on a launch configuration are now either optional or turned into throwable getters * **autoscaling:** `AutoScalingGroupProps.instanceType` is now optional * **autoscaling:** `AutoScalingGroupProps.machineImage` is now optional * **autoscaling:** `AutoScalingGroup.connections` is now a throwable getter * **autoscaling:** `AutoScalingGroup.role` is now a throwable getter * **autoscaling:** `AutoScalingGroup.userData` is now a throwable getter ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 186dc32 commit 1581af0

File tree

10 files changed

+3395
-60
lines changed

10 files changed

+3395
-60
lines changed

packages/@aws-cdk/aws-autoscaling/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@ new autoscaling.AutoScalingGroup(this, 'ASG', {
4646
});
4747
```
4848

49+
Alternatively you can create an `AutoScalingGroup` from a `LaunchTemplate`:
50+
51+
```ts
52+
declare const vpc: ec2.Vpc;
53+
declare const launchTemplate: ec2.LaunchTemplate;
54+
55+
new autoscaling.AutoScalingGroup(this, 'ASG', {
56+
vpc,
57+
launchTemplate: launchTemplate
58+
});
59+
```
60+
61+
To launch a mixture of Spot and on-demand instances, and/or with multiple instance types, you can create an `AutoScalingGroup` from a MixedInstancesPolicy:
62+
63+
```ts
64+
declare const vpc: ec2.Vpc;
65+
declare const launchTemplate1: ec2.LaunchTemplate;
66+
declare const launchTemplate2: ec2.LaunchTemplate;
67+
68+
new autoscaling.AutoScalingGroup(this, 'ASG', {
69+
vpc,
70+
mixedInstancesPolicy: {
71+
instancesDistribution: {
72+
onDemandPercentageAboveBaseCapacity: 50, // Mix Spot and On-Demand instances
73+
},
74+
launchTemplate: launchTemplate1,
75+
launchTemplateOverrides: [ // Mix multiple instance types
76+
{ instanceType: new ec2.InstanceType('t3.micro') },
77+
{ instanceType: new ec2.InstanceType('t3a.micro') },
78+
{ instanceType: new ec2.InstanceType('t4g.micro'), launchTemplate: launchTemplate2 },
79+
],
80+
}
81+
});
82+
```
83+
4984
## Machine Images (AMIs)
5085

5186
AMIs control the OS that gets launched when you start your EC2 instance. The EC2

0 commit comments

Comments
 (0)