From 140cdc94bdeee85010529988755252796a31b1d4 Mon Sep 17 00:00:00 2001 From: Hsing-Hui Hsu Date: Thu, 25 Oct 2018 15:33:58 -0700 Subject: [PATCH 1/2] fix(aws-autoscaling): allow minSize to be set to 0 Previously, minSize was being set to `props.minSize || 1`. Since 0 is falsey in javascript, this would mean 0 passed in through the ASG props evaluate to false and the field would always be set to the default value of 1. This change uses strict equality comparison to allow 0 to be set. --- .../aws-autoscaling/lib/auto-scaling-group.ts | 2 +- .../test/test.auto-scaling-group.ts | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts index 9f82bae8f8098..140f6cc0e00ab 100644 --- a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts @@ -212,7 +212,7 @@ export class AutoScalingGroup extends cdk.Construct implements cdk.ITaggable, el launchConfig.addDependency(this.role); - const minSize = props.minSize || 1; + const minSize = props.minSize !== undefined ? props.minSize : 1; const maxSize = props.maxSize || 1; const desiredCapacity = props.desiredCapacity || 1; diff --git a/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts index a2a63050b9658..d1fdf49841fcd 100644 --- a/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts @@ -126,6 +126,125 @@ export = { test.done(); }, + 'can scale min size to 0'(test: Test) { + const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }}); + const vpc = mockVpc(stack); + + new autoscaling.AutoScalingGroup(stack, 'MyFleet', { + instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro), + machineImage: new ec2.AmazonLinuxImage(), + minSize: 0, + vpc + }); + + expect(stack).toMatch({ + "Resources": { + "MyFleetInstanceSecurityGroup774E8234": { + "Type": "AWS::EC2::SecurityGroup", + "Properties": { + "GroupDescription": "MyFleet/InstanceSecurityGroup", + "SecurityGroupEgress": [ + { + "CidrIp": "0.0.0.0/0", + "Description": "Allow all outbound traffic by default", + "IpProtocol": "-1", + } + ], + "SecurityGroupIngress": [], + "Tags": [ + { + "Key": "Name", + "Value": "MyFleet" + } + ], + + "VpcId": "my-vpc" + } + }, + "MyFleetInstanceRole25A84AB8": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "ec2.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "MyFleetInstanceProfile70A58496": { + "Type": "AWS::IAM::InstanceProfile", + "Properties": { + "Roles": [ + { + "Ref": "MyFleetInstanceRole25A84AB8" + } + ] + } + }, + "MyFleetLaunchConfig5D7F9801": { + "Type": "AWS::AutoScaling::LaunchConfiguration", + "Properties": { + "IamInstanceProfile": { + "Ref": "MyFleetInstanceProfile70A58496" + }, + "ImageId": "dummy", + "InstanceType": "m4.micro", + "SecurityGroups": [ + { + "Fn::GetAtt": [ + "MyFleetInstanceSecurityGroup774E8234", + "GroupId" + ] + } + ], + "UserData": { + "Fn::Base64": "#!/bin/bash\n" + } + }, + "DependsOn": [ + "MyFleetInstanceRole25A84AB8" + ] + }, + "MyFleetASG88E55886": { + "Type": "AWS::AutoScaling::AutoScalingGroup", + "UpdatePolicy": { + "AutoScalingScheduledAction": { + "IgnoreUnmodifiedGroupSizeProperties": true + } + }, + "Properties": { + "DesiredCapacity": "1", + "LaunchConfigurationName": { + "Ref": "MyFleetLaunchConfig5D7F9801" + }, + "Tags": [ + { + "Key": "Name", + "PropagateAtLaunch": true, + "Value": "MyFleet" + } + ], + + "MaxSize": "1", + "MinSize": "0", + "VPCZoneIdentifier": [ + "pri1" + ] + } + } + } + }); + + test.done(); + }, + 'addToRolePolicy can be used to add statements to the role policy'(test: Test) { const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }}); const vpc = mockVpc(stack); From e1ec138095742e0acaa18cd37fd2895038770964 Mon Sep 17 00:00:00 2001 From: Hsing-Hui Hsu Date: Fri, 26 Oct 2018 13:01:09 -0700 Subject: [PATCH 2/2] Also change maxSize and desiredCapacity --- .../aws-autoscaling/lib/auto-scaling-group.ts | 4 +- .../test/test.auto-scaling-group.ts | 114 ++---------------- 2 files changed, 11 insertions(+), 107 deletions(-) diff --git a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts index 140f6cc0e00ab..584240c470701 100644 --- a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts @@ -213,8 +213,8 @@ export class AutoScalingGroup extends cdk.Construct implements cdk.ITaggable, el launchConfig.addDependency(this.role); const minSize = props.minSize !== undefined ? props.minSize : 1; - const maxSize = props.maxSize || 1; - const desiredCapacity = props.desiredCapacity || 1; + const maxSize = props.maxSize !== undefined ? props.maxSize : 1; + const desiredCapacity = props.desiredCapacity !== undefined ? props.desiredCapacity : 1; if (desiredCapacity < minSize || desiredCapacity > maxSize) { throw new Error(`Should have minSize (${minSize}) <= desiredCapacity (${desiredCapacity}) <= maxSize (${maxSize})`); diff --git a/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts index d1fdf49841fcd..587ef5f91b22b 100644 --- a/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts @@ -126,121 +126,25 @@ export = { test.done(); }, - 'can scale min size to 0'(test: Test) { + 'can set minSize, maxSize, desiredCapacity to 0'(test: Test) { const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }}); const vpc = mockVpc(stack); new autoscaling.AutoScalingGroup(stack, 'MyFleet', { instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro), machineImage: new ec2.AmazonLinuxImage(), + vpc, minSize: 0, - vpc + maxSize: 0, + desiredCapacity: 0 }); - expect(stack).toMatch({ - "Resources": { - "MyFleetInstanceSecurityGroup774E8234": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": { - "GroupDescription": "MyFleet/InstanceSecurityGroup", - "SecurityGroupEgress": [ - { - "CidrIp": "0.0.0.0/0", - "Description": "Allow all outbound traffic by default", - "IpProtocol": "-1", - } - ], - "SecurityGroupIngress": [], - "Tags": [ - { - "Key": "Name", - "Value": "MyFleet" - } - ], - - "VpcId": "my-vpc" - } - }, - "MyFleetInstanceRole25A84AB8": { - "Type": "AWS::IAM::Role", - "Properties": { - "AssumeRolePolicyDocument": { - "Statement": [ - { - "Action": "sts:AssumeRole", - "Effect": "Allow", - "Principal": { - "Service": "ec2.amazonaws.com" - } - } - ], - "Version": "2012-10-17" - } - } - }, - "MyFleetInstanceProfile70A58496": { - "Type": "AWS::IAM::InstanceProfile", - "Properties": { - "Roles": [ - { - "Ref": "MyFleetInstanceRole25A84AB8" - } - ] - } - }, - "MyFleetLaunchConfig5D7F9801": { - "Type": "AWS::AutoScaling::LaunchConfiguration", - "Properties": { - "IamInstanceProfile": { - "Ref": "MyFleetInstanceProfile70A58496" - }, - "ImageId": "dummy", - "InstanceType": "m4.micro", - "SecurityGroups": [ - { - "Fn::GetAtt": [ - "MyFleetInstanceSecurityGroup774E8234", - "GroupId" - ] - } - ], - "UserData": { - "Fn::Base64": "#!/bin/bash\n" - } - }, - "DependsOn": [ - "MyFleetInstanceRole25A84AB8" - ] - }, - "MyFleetASG88E55886": { - "Type": "AWS::AutoScaling::AutoScalingGroup", - "UpdatePolicy": { - "AutoScalingScheduledAction": { - "IgnoreUnmodifiedGroupSizeProperties": true - } - }, - "Properties": { - "DesiredCapacity": "1", - "LaunchConfigurationName": { - "Ref": "MyFleetLaunchConfig5D7F9801" - }, - "Tags": [ - { - "Key": "Name", - "PropagateAtLaunch": true, - "Value": "MyFleet" - } - ], - - "MaxSize": "1", - "MinSize": "0", - "VPCZoneIdentifier": [ - "pri1" - ] - } - } + expect(stack).to(haveResource("AWS::AutoScaling::AutoScalingGroup", { + MinSize: "0", + MaxSize: "0", + DesiredCapacity: "0", } - }); + )); test.done(); },