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

Commit

Permalink
Replace target tracking scaling with simple scaling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
akash1810 committed Sep 2, 2024
1 parent e990c09 commit f13e703
Show file tree
Hide file tree
Showing 2 changed files with 72 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

0 comments on commit f13e703

Please sign in to comment.