Skip to content

Commit

Permalink
fix(autoscaling): every deployment resets capacity (#5507)
Browse files Browse the repository at this point in the history
* fix(autoscaling): every deployment resets capacity

If `DesiredCapacity` is specified in the CloudFormation template, on every
deployment the capacity of the AutoScalingGroup is reset to that number,
even if the group had been scaled out at that point. The solution is to
leave DesiredCapacity empty, in which case it will remain untouched
during a deployment.

Previously, CDK would use some logic to always calculate a
DesiredCapacity for you, even if you left the `desiredCapacity` property
unset, leading to the undesirable behavior--which frankly represents
an availability risk.

Now, if you don't specify `desiredCapacity`, we won't set
`DesiredCapacity` either, avoiding the availability risk that we
introduced beforehand. In fact, if you *do* set `desiredCapacity`, we
will warn you that you probably shouldn't using a construct warning.

Fixes #5215, closes #5208.

BREAKING CHANGE: AutoScalingGroups without `desiredCapacity` are now
initially scaled to their minimum capacity (instead of their maximum
capaciety).

* Add links

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
rix0rrr and mergify[bot] committed Dec 23, 2019
1 parent c388ad6 commit 0adf6c7
Show file tree
Hide file tree
Showing 24 changed files with 138 additions and 50 deletions.
28 changes: 21 additions & 7 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export interface CommonAutoScalingGroupProps {
/**
* Initial amount of instances in the fleet
*
* @default 1
* If this is set to a number, every deployment will reset the amount of
* instances to this number. It is recommended to leave this value blank.
*
* @default minCapacity, and leave unchanged during deployment
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-desiredcapacity
*/
readonly desiredCapacity?: number;

Expand Down Expand Up @@ -475,30 +479,40 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements

launchConfig.node.addDependency(this.role);

const desiredCapacity =
(props.desiredCapacity !== undefined ? props.desiredCapacity :
(props.minCapacity !== undefined ? props.minCapacity :
(props.maxCapacity !== undefined ? props.maxCapacity : 1)));
// desiredCapacity just reflects what the user has supplied.
const desiredCapacity = props.desiredCapacity;
const minCapacity = props.minCapacity !== undefined ? props.minCapacity : 1;
const maxCapacity = props.maxCapacity !== undefined ? props.maxCapacity : desiredCapacity;
const maxCapacity = props.maxCapacity !== undefined ? props.maxCapacity :
desiredCapacity !== undefined ? desiredCapacity : Math.max(minCapacity, 1);

withResolved(minCapacity, maxCapacity, (min, max) => {
if (min > max) {
throw new Error(`minCapacity (${min}) should be <= maxCapacity (${max})`);
}
});
withResolved(desiredCapacity, minCapacity, (desired, min) => {
if (desired === undefined) { return; }
if (desired < min) {
throw new Error(`Should have minCapacity (${min}) <= desiredCapacity (${desired})`);
}
});
withResolved(desiredCapacity, maxCapacity, (desired, max) => {
if (desired === undefined) { return; }
if (max < desired) {
throw new Error(`Should have desiredCapacity (${desired}) <= maxCapacity (${max})`);
}
});

if (desiredCapacity !== undefined) {
this.node.addWarning(`desiredCapacity has been configured. Be aware this will reset the size of your AutoScalingGroup on every deployment. See https://github.com/aws/aws-cdk/issues/5215`);
}

const { subnetIds, hasPublic } = props.vpc.selectSubnets(props.vpcSubnets);
const asgProps: CfnAutoScalingGroupProps = {
cooldown: props.cooldown !== undefined ? props.cooldown.toSeconds().toString() : undefined,
minSize: Tokenization.stringifyNumber(minCapacity),
maxSize: Tokenization.stringifyNumber(maxCapacity),
desiredCapacity: Tokenization.stringifyNumber(desiredCapacity),
desiredCapacity: desiredCapacity !== undefined ? Tokenization.stringifyNumber(desiredCapacity) : undefined,
launchConfigurationName: launchConfig.ref,
loadBalancerNames: Lazy.listValue({ produce: () => this.loadBalancerNames }, { omitEmpty: true }),
targetGroupArns: Lazy.listValue({ produce: () => this.targetGroupArns }, { omitEmpty: true }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "FleetLaunchConfig59F79D36"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "FleetLaunchConfig59F79D36"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "FleetLaunchConfig59F79D36"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "FleetLaunchConfig59F79D36"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "ASGLaunchConfigC00AF12B"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "FleetLaunchConfig59F79D36"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export = {
}
},
"Properties": {
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "MyFleetLaunchConfig5D7F9801"
},
Expand Down Expand Up @@ -252,7 +251,6 @@ export = {
expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", {
MinSize: "10",
MaxSize: "10",
DesiredCapacity: "10",
}
));

Expand All @@ -275,8 +273,7 @@ export = {
// THEN
expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", {
MinSize: "1",
MaxSize: "10",
DesiredCapacity: "10",
MaxSize: "10"
}
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export = {
Properties: {
MaxSize: "1",
MinSize: "1",
DesiredCapacity: "1",
LaunchConfigurationName: { Ref: "ASGLaunchConfigC00AF12B" },
Tags: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "ASGLaunchConfigC00AF12B"
},
Expand Down Expand Up @@ -885,4 +884,4 @@
"Default": "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "ClusterDefaultAutoScalingGroupLaunchConfig81EA5466"
},
Expand Down Expand Up @@ -1152,4 +1151,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -856,4 +855,4 @@
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -939,4 +938,4 @@
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -987,4 +986,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -1053,4 +1052,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -1017,4 +1016,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -963,4 +962,4 @@
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -928,4 +927,4 @@
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1323,4 +1323,4 @@
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
}
}
}
4 changes: 0 additions & 4 deletions packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export = {
expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", {
MaxSize: "1",
MinSize: "1",
DesiredCapacity: "1",
LaunchConfigurationName: {
Ref: "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -213,7 +212,6 @@ export = {
expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", {
MaxSize: "1",
MinSize: "1",
DesiredCapacity: "1",
LaunchConfigurationName: {
Ref: "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -514,7 +512,6 @@ export = {
expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", {
MaxSize: "1",
MinSize: "1",
DesiredCapacity: "1",
LaunchConfigurationName: {
Ref: "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -1273,7 +1270,6 @@ export = {
expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", {
MaxSize: "1",
MinSize: "1",
DesiredCapacity: "1",
LaunchConfigurationName: {
Ref: "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/@aws-cdk/aws-eks/test/integ.eks-spot.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,6 @@
"Properties": {
"MaxSize": "10",
"MinSize": "1",
"DesiredCapacity": "10",
"LaunchConfigurationName": {
"Ref": "myClusterspotLaunchConfig6681F311"
},
Expand Down Expand Up @@ -1449,4 +1448,4 @@
"Default": "/aws/service/eks/optimized-ami/1.14/amazon-linux-2/recommended/image_id"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "EcsClusterDefaultAutoScalingGroupLaunchConfigB7E376C1"
},
Expand Down Expand Up @@ -1125,4 +1124,4 @@
"Description": "Artifact hash for asset \"ea7034d81c091be1158bcd85b4958dc86ec6672c345be27607d68fdfcf26b1c1\""
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
"Properties": {
"MaxSize": "1",
"MinSize": "1",
"DesiredCapacity": "1",
"LaunchConfigurationName": {
"Ref": "FargateClusterDefaultAutoScalingGroupLaunchConfig57306899"
},
Expand Down
Loading

0 comments on commit 0adf6c7

Please sign in to comment.