Skip to content

Commit

Permalink
feat(autoscaling): Auto Scaling Group with Launch Template (aws#19066)
Browse files Browse the repository at this point in the history
Add support to launch an Auto Scaling Group from a Launch Template rather than a launch configuration.

Closes aws#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*
  • Loading branch information
Martin1994 authored and Stephen Potter committed Apr 27, 2022
1 parent 457def8 commit 0248bea
Show file tree
Hide file tree
Showing 10 changed files with 3,395 additions and 60 deletions.
35 changes: 35 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,41 @@ new autoscaling.AutoScalingGroup(this, 'ASG', {
});
```

Alternatively you can create an `AutoScalingGroup` from a `LaunchTemplate`:

```ts
declare const vpc: ec2.Vpc;
declare const launchTemplate: ec2.LaunchTemplate;

new autoscaling.AutoScalingGroup(this, 'ASG', {
vpc,
launchTemplate: launchTemplate
});
```

To launch a mixture of Spot and on-demand instances, and/or with multiple instance types, you can create an `AutoScalingGroup` from a MixedInstancesPolicy:

```ts
declare const vpc: ec2.Vpc;
declare const launchTemplate1: ec2.LaunchTemplate;
declare const launchTemplate2: ec2.LaunchTemplate;

new autoscaling.AutoScalingGroup(this, 'ASG', {
vpc,
mixedInstancesPolicy: {
instancesDistribution: {
onDemandPercentageAboveBaseCapacity: 50, // Mix Spot and On-Demand instances
},
launchTemplate: launchTemplate1,
launchTemplateOverrides: [ // Mix multiple instance types
{ instanceType: new ec2.InstanceType('t3.micro') },
{ instanceType: new ec2.InstanceType('t3a.micro') },
{ instanceType: new ec2.InstanceType('t4g.micro'), launchTemplate: launchTemplate2 },
],
}
});
```

## Machine Images (AMIs)

AMIs control the OS that gets launched when you start your EC2 instance. The EC2
Expand Down
Loading

0 comments on commit 0248bea

Please sign in to comment.