Skip to content

Commit 0adf6c7

Browse files
rix0rrrmergify[bot]
andcommitted
fix(autoscaling): every deployment resets capacity (#5507)
* 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>
1 parent c388ad6 commit 0adf6c7

24 files changed

+138
-50
lines changed

packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export interface CommonAutoScalingGroupProps {
4444
/**
4545
* Initial amount of instances in the fleet
4646
*
47-
* @default 1
47+
* If this is set to a number, every deployment will reset the amount of
48+
* instances to this number. It is recommended to leave this value blank.
49+
*
50+
* @default minCapacity, and leave unchanged during deployment
51+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-desiredcapacity
4852
*/
4953
readonly desiredCapacity?: number;
5054

@@ -475,30 +479,40 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
475479

476480
launchConfig.node.addDependency(this.role);
477481

478-
const desiredCapacity =
479-
(props.desiredCapacity !== undefined ? props.desiredCapacity :
480-
(props.minCapacity !== undefined ? props.minCapacity :
481-
(props.maxCapacity !== undefined ? props.maxCapacity : 1)));
482+
// desiredCapacity just reflects what the user has supplied.
483+
const desiredCapacity = props.desiredCapacity;
482484
const minCapacity = props.minCapacity !== undefined ? props.minCapacity : 1;
483-
const maxCapacity = props.maxCapacity !== undefined ? props.maxCapacity : desiredCapacity;
485+
const maxCapacity = props.maxCapacity !== undefined ? props.maxCapacity :
486+
desiredCapacity !== undefined ? desiredCapacity : Math.max(minCapacity, 1);
484487

488+
withResolved(minCapacity, maxCapacity, (min, max) => {
489+
if (min > max) {
490+
throw new Error(`minCapacity (${min}) should be <= maxCapacity (${max})`);
491+
}
492+
});
485493
withResolved(desiredCapacity, minCapacity, (desired, min) => {
494+
if (desired === undefined) { return; }
486495
if (desired < min) {
487496
throw new Error(`Should have minCapacity (${min}) <= desiredCapacity (${desired})`);
488497
}
489498
});
490499
withResolved(desiredCapacity, maxCapacity, (desired, max) => {
500+
if (desired === undefined) { return; }
491501
if (max < desired) {
492502
throw new Error(`Should have desiredCapacity (${desired}) <= maxCapacity (${max})`);
493503
}
494504
});
495505

506+
if (desiredCapacity !== undefined) {
507+
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`);
508+
}
509+
496510
const { subnetIds, hasPublic } = props.vpc.selectSubnets(props.vpcSubnets);
497511
const asgProps: CfnAutoScalingGroupProps = {
498512
cooldown: props.cooldown !== undefined ? props.cooldown.toSeconds().toString() : undefined,
499513
minSize: Tokenization.stringifyNumber(minCapacity),
500514
maxSize: Tokenization.stringifyNumber(maxCapacity),
501-
desiredCapacity: Tokenization.stringifyNumber(desiredCapacity),
515+
desiredCapacity: desiredCapacity !== undefined ? Tokenization.stringifyNumber(desiredCapacity) : undefined,
502516
launchConfigurationName: launchConfig.ref,
503517
loadBalancerNames: Lazy.listValue({ produce: () => this.loadBalancerNames }, { omitEmpty: true }),
504518
targetGroupArns: Lazy.listValue({ produce: () => this.targetGroupArns }, { omitEmpty: true }),

packages/@aws-cdk/aws-autoscaling/test/integ.amazonlinux2.expected.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@
451451
"Properties": {
452452
"MaxSize": "1",
453453
"MinSize": "1",
454-
"DesiredCapacity": "1",
455454
"LaunchConfigurationName": {
456455
"Ref": "FleetLaunchConfig59F79D36"
457456
},

packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,6 @@
631631
"Properties": {
632632
"MaxSize": "1",
633633
"MinSize": "1",
634-
"DesiredCapacity": "1",
635634
"LaunchConfigurationName": {
636635
"Ref": "FleetLaunchConfig59F79D36"
637636
},

packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-elbv2.expected.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@
472472
"Properties": {
473473
"MaxSize": "1",
474474
"MinSize": "1",
475-
"DesiredCapacity": "1",
476475
"LaunchConfigurationName": {
477476
"Ref": "FleetLaunchConfig59F79D36"
478477
},

packages/@aws-cdk/aws-autoscaling/test/integ.custom-scaling.expected.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@
451451
"Properties": {
452452
"MaxSize": "1",
453453
"MinSize": "1",
454-
"DesiredCapacity": "1",
455454
"LaunchConfigurationName": {
456455
"Ref": "FleetLaunchConfig59F79D36"
457456
},

packages/@aws-cdk/aws-autoscaling/test/integ.external-role.expected.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@
604604
"Properties": {
605605
"MaxSize": "1",
606606
"MinSize": "1",
607-
"DesiredCapacity": "1",
608607
"LaunchConfigurationName": {
609608
"Ref": "ASGLaunchConfigC00AF12B"
610609
},

packages/@aws-cdk/aws-autoscaling/test/integ.spot-instances.expected.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@
452452
"Properties": {
453453
"MaxSize": "1",
454454
"MinSize": "1",
455-
"DesiredCapacity": "1",
456455
"LaunchConfigurationName": {
457456
"Ref": "FleetLaunchConfig59F79D36"
458457
},

packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ export = {
113113
}
114114
},
115115
"Properties": {
116-
"DesiredCapacity": "1",
117116
"LaunchConfigurationName": {
118117
"Ref": "MyFleetLaunchConfig5D7F9801"
119118
},
@@ -252,7 +251,6 @@ export = {
252251
expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", {
253252
MinSize: "10",
254253
MaxSize: "10",
255-
DesiredCapacity: "10",
256254
}
257255
));
258256

@@ -275,8 +273,7 @@ export = {
275273
// THEN
276274
expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", {
277275
MinSize: "1",
278-
MaxSize: "10",
279-
DesiredCapacity: "10",
276+
MaxSize: "10"
280277
}
281278
));
282279

packages/@aws-cdk/aws-autoscaling/test/test.scheduled-action.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export = {
6464
Properties: {
6565
MaxSize: "1",
6666
MinSize: "1",
67-
DesiredCapacity: "1",
6867
LaunchConfigurationName: { Ref: "ASGLaunchConfigC00AF12B" },
6968
Tags: [
7069
{

packages/@aws-cdk/aws-codedeploy/test/server/integ.deployment-group.expected.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,6 @@
683683
"Properties": {
684684
"MaxSize": "1",
685685
"MinSize": "1",
686-
"DesiredCapacity": "1",
687686
"LaunchConfigurationName": {
688687
"Ref": "ASGLaunchConfigC00AF12B"
689688
},
@@ -885,4 +884,4 @@
885884
"Default": "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2"
886885
}
887886
}
888-
}
887+
}

0 commit comments

Comments
 (0)