Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autoscaling): every deployment resets capacity #5507

Merged
merged 5 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 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,10 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this accurate, it doesn't look like you're defaulting desiredCapacity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This describes the end-to-end behavior of what will happen if you leave the value empty.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-desiredcapacity

If you do not specify a desired capacity, the default is the minimum size of the group.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this link in the docstring

*/
readonly desiredCapacity?: number;

Expand Down Expand Up @@ -475,30 +478,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.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provider a link to a github issue in the help message so people can dive deeper/comment.

}

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