Skip to content

Commit

Permalink
refactor: Add alarm for step scaling policy
Browse files Browse the repository at this point in the history
Co-authored-by: Ravi <[email protected]>
  • Loading branch information
ioannakok and arelra committed Jan 2, 2024
1 parent 189c6bc commit 023c272
Showing 1 changed file with 55 additions and 29 deletions.
84 changes: 55 additions & 29 deletions dotcom-rendering/cdk/lib/dotcom-rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@ import {
import { GuClassicLoadBalancer } from '@guardian/cdk/lib/constructs/loadbalancing';
import type { App } from 'aws-cdk-lib';
import { CfnOutput, Duration, Tags } from 'aws-cdk-lib';
import { AdjustmentType, HealthCheck } from 'aws-cdk-lib/aws-autoscaling';
import { CfnAlarm, Metric } from 'aws-cdk-lib/aws-cloudwatch';
import {
AdjustmentType,
HealthCheck,
StepScalingAction,
} from 'aws-cdk-lib/aws-autoscaling';
import {
Alarm,
CfnAlarm,
ComparisonOperator,
Metric,
TreatMissingData,
} from 'aws-cdk-lib/aws-cloudwatch';
import { InstanceType, Peer } from 'aws-cdk-lib/aws-ec2';
import { LoadBalancingProtocol } from 'aws-cdk-lib/aws-elasticloadbalancing';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import type { DCRAlarmConfig, DCRProps } from './types';
import { getUserData } from './userData';
import { AutoScalingAction } from 'aws-cdk-lib/aws-cloudwatch-actions';

Check failure on line 37 in dotcom-rendering/cdk/lib/dotcom-rendering.ts

View workflow job for this annotation

GitHub Actions / lint / check

`aws-cdk-lib/aws-cloudwatch-actions` import should occur before import of `aws-cdk-lib/aws-ec2`

/**
* DCR infrastructure provisioning via CDK
Expand Down Expand Up @@ -280,36 +291,51 @@ export class DotcomRendering extends GuStack {
// scalingAdjustment: -1,
// });

const latencyMetric = new Metric({
metricName: 'Latency',
namespace: 'AWS/ELB',
period: Duration.minutes(1),
statistic: 'Average',
const latencyHighAlarm = new Alarm(this, 'HighLatency', {
actionsEnabled: true,
alarmDescription: 'Latency alarm for autoscaling',
threshold: 0.2,
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
datapointsToAlarm: 2,
evaluationPeriods: 30,
metric: new Metric({
dimensionsMap: {
AutoScalingGroupName: asg.autoScalingGroupName,
},
metricName: 'Latency',
namespace: 'AWS/ELB',
period: Duration.seconds(1),
statistic: 'Average',
}),

treatMissingData: TreatMissingData.MISSING,
});

asg.scaleOnMetric('LatencyStepScalingPolicy', {
metric: latencyMetric,
scalingSteps: [
{
lower: 0,
upper: 0.2,
change: props.minCapacity,
},
{
lower: 0.2,
upper: 0.3,
change: props.minCapacity + 3,
},
{
lower: 0.3,
upper: undefined,
change: props.minCapacity + 6,
},
],
adjustmentType: AdjustmentType.EXACT_CAPACITY,
cooldown: Duration.minutes(2),
evaluationPeriods: 1,
const scaleUpStep = new StepScalingAction(this, 'ScaleUp', {
adjustmentType: AdjustmentType.PERCENT_CHANGE_IN_CAPACITY,
autoScalingGroup: asg,
// 10 minutes for prod. This is only for testing
cooldown: Duration.seconds(30),
});

scaleUpStep.addAdjustment({
lowerBound: 0,
adjustment: 100,
});
latencyHighAlarm.addAlarmAction(new AutoScalingAction(scaleUpStep));

const scaleDownStep = new StepScalingAction(this, 'ScaleDown', {
adjustmentType: AdjustmentType.CHANGE_IN_CAPACITY,
autoScalingGroup: asg,

// Every 2 minutes take out one instance for prod. This is for testing purposes
cooldown: Duration.seconds(15),
});
scaleDownStep.addAdjustment({
lowerBound: 0,
adjustment: -1,
});
latencyHighAlarm.addOkAction(new AutoScalingAction(scaleDownStep));

/** Returns an appropriate alarm description given the appropriate configuration object */
const getAlarmDescription = ({
Expand Down

0 comments on commit 023c272

Please sign in to comment.