Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from guardian/aa/scaling
Browse files Browse the repository at this point in the history
Use simple scaling for easier testing
  • Loading branch information
akash1810 authored Sep 2, 2024
2 parents e990c09 + 4a2be1b commit 614a024
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 76 deletions.
116 changes: 43 additions & 73 deletions packages/cdk/lib/__snapshots__/scaling-asg-rolling-update.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
32 changes: 29 additions & 3 deletions packages/cdk/lib/scaling-asg-rolling-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions script/scale-in
Original file line number Diff line number Diff line change
@@ -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"
46 changes: 46 additions & 0 deletions script/scale-out
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 614a024

Please sign in to comment.