From f13e703b8d19bfc4c00417ec9917d5fc8bdb7a8b Mon Sep 17 00:00:00 2001 From: akash1810 Date: Mon, 2 Sep 2024 15:10:01 +0100 Subject: [PATCH 1/2] Replace target tracking scaling with simple scaling Using simple scaling should allow us to utilise https://docs.aws.amazon.com/cli/latest/reference/autoscaling/execute-policy.html to test behaviour more deterministically. --- .../scaling-asg-rolling-update.test.ts.snap | 116 +++++++----------- .../cdk/lib/scaling-asg-rolling-update.ts | 32 ++++- 2 files changed, 72 insertions(+), 76 deletions(-) diff --git a/packages/cdk/lib/__snapshots__/scaling-asg-rolling-update.test.ts.snap b/packages/cdk/lib/__snapshots__/scaling-asg-rolling-update.test.ts.snap index 332074e..6f15caa 100644 --- a/packages/cdk/lib/__snapshots__/scaling-asg-rolling-update.test.ts.snap +++ b/packages/cdk/lib/__snapshots__/scaling-asg-rolling-update.test.ts.snap @@ -29,6 +29,11 @@ exports[`The ScalingAsgRollingUpdate stack matches the snapshot 1`] = ` "gu:cdk:version": "TEST", }, "Outputs": { + "AutoscalingGroupName": { + "Value": { + "Ref": "AutoScalingGroupScalingASG8AF02C37", + }, + }, "LoadBalancerScalingDnsName": { "Description": "DNS entry for LoadBalancerScaling", "Value": { @@ -38,6 +43,22 @@ exports[`The ScalingAsgRollingUpdate stack matches the snapshot 1`] = ` ], }, }, + "ScaleInArn": { + "Value": { + "Fn::GetAtt": [ + "ScaleIn", + "Arn", + ], + }, + }, + "ScaleOutArn": { + "Value": { + "Fn::GetAtt": [ + "ScaleOut", + "Arn", + ], + }, + }, }, "Parameters": { "AMIScaling": { @@ -192,79 +213,6 @@ exports[`The ScalingAsgRollingUpdate stack matches the snapshot 1`] = ` }, }, }, - "AutoScalingGroupScalingScalingPolicyScaleOnRequest26CCAA7B": { - "DependsOn": [ - "ListenerScaling76B5CBA7", - ], - "Properties": { - "AutoScalingGroupName": { - "Ref": "AutoScalingGroupScalingASG8AF02C37", - }, - "PolicyType": "TargetTrackingScaling", - "TargetTrackingConfiguration": { - "PredefinedMetricSpecification": { - "PredefinedMetricType": "ALBRequestCountPerTarget", - "ResourceLabel": { - "Fn::Join": [ - "", - [ - { - "Fn::Select": [ - 1, - { - "Fn::Split": [ - "/", - { - "Ref": "ListenerScaling76B5CBA7", - }, - ], - }, - ], - }, - "/", - { - "Fn::Select": [ - 2, - { - "Fn::Split": [ - "/", - { - "Ref": "ListenerScaling76B5CBA7", - }, - ], - }, - ], - }, - "/", - { - "Fn::Select": [ - 3, - { - "Fn::Split": [ - "/", - { - "Ref": "ListenerScaling76B5CBA7", - }, - ], - }, - ], - }, - "/", - { - "Fn::GetAtt": [ - "TargetGroupScalingFC90421F", - "TargetGroupFullName", - ], - }, - ], - ], - }, - }, - "TargetValue": 5, - }, - }, - "Type": "AWS::AutoScaling::ScalingPolicy", - }, "CertificateScaling767DD870": { "DeletionPolicy": "Retain", "Properties": { @@ -727,6 +675,28 @@ exports[`The ScalingAsgRollingUpdate stack matches the snapshot 1`] = ` }, "Type": "AWS::IAM::Policy", }, + "ScaleIn": { + "Properties": { + "AdjustmentType": "ChangeInCapacity", + "AutoScalingGroupName": { + "Ref": "AutoScalingGroupScalingASG8AF02C37", + }, + "PolicyType": "SimpleScaling", + "ScalingAdjustment": -1, + }, + "Type": "AWS::AutoScaling::ScalingPolicy", + }, + "ScaleOut": { + "Properties": { + "AdjustmentType": "ChangeInCapacity", + "AutoScalingGroupName": { + "Ref": "AutoScalingGroupScalingASG8AF02C37", + }, + "PolicyType": "SimpleScaling", + "ScalingAdjustment": 1, + }, + "Type": "AWS::AutoScaling::ScalingPolicy", + }, "SsmSshPolicy4CFC977E": { "Properties": { "PolicyDocument": { diff --git a/packages/cdk/lib/scaling-asg-rolling-update.ts b/packages/cdk/lib/scaling-asg-rolling-update.ts index 8579232..71d01b9 100644 --- a/packages/cdk/lib/scaling-asg-rolling-update.ts +++ b/packages/cdk/lib/scaling-asg-rolling-update.ts @@ -3,8 +3,9 @@ import { GuStack } from '@guardian/cdk/lib/constructs/core'; import { GuCname } from '@guardian/cdk/lib/constructs/dns'; import { GuEc2AppExperimental } from '@guardian/cdk/lib/experimental/patterns/ec2-app'; import type { App } from 'aws-cdk-lib'; -import { Duration, Tags } from 'aws-cdk-lib'; +import { CfnOutput, Duration, Tags } from 'aws-cdk-lib'; import type { CfnAutoScalingGroup } from 'aws-cdk-lib/aws-autoscaling'; +import { CfnScalingPolicy } from 'aws-cdk-lib/aws-autoscaling'; import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2'; interface ScalingAsgRollingUpdateProps { @@ -61,8 +62,33 @@ export class ScalingAsgRollingUpdate extends GuStack { 'testing-asg-rolling-update.service', ); - autoScalingGroup.scaleOnRequestCount('ScaleOnRequest', { - targetRequestsPerMinute: 5, + const scaleOutPolicy = new CfnScalingPolicy(this, 'ScaleOut', { + autoScalingGroupName: autoScalingGroup.autoScalingGroupName, + policyType: 'SimpleScaling', + adjustmentType: 'ChangeInCapacity', + scalingAdjustment: 1, + }); + + const scaleInPolicy = new CfnScalingPolicy(this, 'ScaleIn', { + autoScalingGroupName: autoScalingGroup.autoScalingGroupName, + policyType: 'SimpleScaling', + adjustmentType: 'ChangeInCapacity', + scalingAdjustment: -1, + }); + + new CfnOutput(this, 'ScaleOutArn', { + key: 'ScaleOutArn', + value: scaleOutPolicy.attrArn, + }); + + new CfnOutput(this, 'ScaleInArn', { + key: 'ScaleInArn', + value: scaleInPolicy.attrArn, + }); + + new CfnOutput(this, 'AutoscalingGroupName', { + key: 'AutoscalingGroupName', + value: autoScalingGroup.autoScalingGroupName, }); const cfnAsg = autoScalingGroup.node.defaultChild as CfnAutoScalingGroup; From 4a2be1b95d8c04b3e3d6371e1af87ba52374857c Mon Sep 17 00:00:00 2001 From: akash1810 Date: Mon, 2 Sep 2024 15:22:30 +0100 Subject: [PATCH 2/2] Add scripts to trigger scale in/out --- script/scale-in | 46 ++++++++++++++++++++++++++++++++++++++++++++++ script/scale-out | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100755 script/scale-in create mode 100755 script/scale-out diff --git a/script/scale-in b/script/scale-in new file mode 100755 index 0000000..0cc1ffe --- /dev/null +++ b/script/scale-in @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +set -e + +CLOUDFORMATION_STACK_NAME=playground-CODE-scaling-asg-rolling-update + +POLICY_ARN=$( + aws cloudformation describe-stacks \ + --stack-name "$CLOUDFORMATION_STACK_NAME" \ + --profile developerPlayground \ + --region eu-west-1 \ + --no-cli-pager | \ + jq -r '.Stacks[].Outputs[] | select( [.OutputKey | contains("ScaleInArn") ] | any) | .OutputValue' +) + +ASG_NAME=$( + aws cloudformation describe-stacks \ + --stack-name "$CLOUDFORMATION_STACK_NAME" \ + --profile developerPlayground \ + --region eu-west-1 \ + --no-cli-pager | \ + jq -r '.Stacks[].Outputs[] | select( [.OutputKey | contains("AutoscalingGroupName") ] | any) | .OutputValue' +) + +CURRENT_DESIRED_CAPACITY=$( + aws autoscaling describe-auto-scaling-groups \ + --auto-scaling-group-names "$ASG_NAME" \ + --profile developerPlayground \ + --region eu-west-1 | \ + jq -r '.AutoScalingGroups[].DesiredCapacity' +) + +aws autoscaling execute-policy \ + --policy-name "$POLICY_ARN" \ + --profile developerPlayground \ + --region eu-west-1 + +NEW_DESIRED_CAPACITY=$( + aws autoscaling describe-auto-scaling-groups \ + --auto-scaling-group-names "$ASG_NAME" \ + --profile developerPlayground \ + --region eu-west-1 | \ + jq -r '.AutoScalingGroups[].DesiredCapacity' +) + +echo "Desired capacity has been updated from $CURRENT_DESIRED_CAPACITY to $NEW_DESIRED_CAPACITY" diff --git a/script/scale-out b/script/scale-out new file mode 100755 index 0000000..c348137 --- /dev/null +++ b/script/scale-out @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +set -e + +CLOUDFORMATION_STACK_NAME=playground-CODE-scaling-asg-rolling-update + +POLICY_ARN=$( + aws cloudformation describe-stacks \ + --stack-name "$CLOUDFORMATION_STACK_NAME" \ + --profile developerPlayground \ + --region eu-west-1 \ + --no-cli-pager | \ + jq -r '.Stacks[].Outputs[] | select( [.OutputKey | contains("ScaleOutArn") ] | any) | .OutputValue' +) + +ASG_NAME=$( + aws cloudformation describe-stacks \ + --stack-name "$CLOUDFORMATION_STACK_NAME" \ + --profile developerPlayground \ + --region eu-west-1 \ + --no-cli-pager | \ + jq -r '.Stacks[].Outputs[] | select( [.OutputKey | contains("AutoscalingGroupName") ] | any) | .OutputValue' +) + +CURRENT_DESIRED_CAPACITY=$( + aws autoscaling describe-auto-scaling-groups \ + --auto-scaling-group-names "$ASG_NAME" \ + --profile developerPlayground \ + --region eu-west-1 | \ + jq -r '.AutoScalingGroups[].DesiredCapacity' +) + +aws autoscaling execute-policy \ + --policy-name "$POLICY_ARN" \ + --profile developerPlayground \ + --region eu-west-1 + +NEW_DESIRED_CAPACITY=$( + aws autoscaling describe-auto-scaling-groups \ + --auto-scaling-group-names "$ASG_NAME" \ + --profile developerPlayground \ + --region eu-west-1 | \ + jq -r '.AutoScalingGroups[].DesiredCapacity' +) + +echo "Desired capacity has been updated from $CURRENT_DESIRED_CAPACITY to $NEW_DESIRED_CAPACITY"