Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: migrate latency scaling alarm to cdk #8664

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ exports[`The DotcomRendering stack matches the snapshot 1`] = `
"PROD",
],
},
"HasLatencyScalingAlarm": {
"Fn::Equals": [
{
"Ref": "Stage",
},
"PROD",
],
},
},
"Description": "Frontend rendering service",
"Mappings": {
Expand Down Expand Up @@ -847,17 +839,17 @@ exports[`The DotcomRendering stack matches the snapshot 1`] = `
"Type": "AWS::EC2::SecurityGroup",
},
"LatencyScalingAlarm": {
"Condition": "HasLatencyScalingAlarm",
"Properties": {
"ActionsEnabled": false,
"AlarmActions": [
{
"Ref": "ScaleUpPolicy",
"Fn::GetAtt": [
"ScaleUpPolicy",
"Arn",
],
},
],
"AlarmDescription": {
"Fn::Sub": "Scale-Up if latency is greater than 0.2 seconds over 1 period(s) of 60 seconds
",
},
"AlarmDescription": "Scale-Up if latency is greater than 0.2 seconds over 1 period(s) of 60 seconds",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Dimensions": [
{
Expand All @@ -872,7 +864,10 @@ exports[`The DotcomRendering stack matches the snapshot 1`] = `
"Namespace": "AWS/ELB",
"OKActions": [
{
"Ref": "ScaleDownPolicy",
"Fn::GetAtt": [
"ScaleDownPolicy",
"Arn",
],
},
],
"Period": 60,
Expand Down
25 changes: 25 additions & 0 deletions dotcom-rendering/cdk/lib/dotcom-rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
CfnScalingPolicy,
HealthCheck,
} from 'aws-cdk-lib/aws-autoscaling';
import { CfnAlarm } from 'aws-cdk-lib/aws-cloudwatch';
import { InstanceType, Peer } from 'aws-cdk-lib/aws-ec2';
import { LoadBalancingProtocol } from 'aws-cdk-lib/aws-elasticloadbalancing';
import { CfnInclude } from 'aws-cdk-lib/cloudformation-include';
Expand Down Expand Up @@ -240,6 +241,30 @@ export class DotcomRendering extends GuStack {
scalingAdjustment: -1,
});

const latencyScalingAlarmThreshold = 0.2;
const latencyScalingAlarmEvaluationPeriod = 1;
const latencyScalingAlarmPeriod = 60;

new CfnAlarm(this, 'LatencyScalingAlarm', {
actionsEnabled: stage === 'PROD',
alarmDescription: `Scale-Up if latency is greater than ${latencyScalingAlarmThreshold} seconds over ${latencyScalingAlarmEvaluationPeriod} period(s) of ${latencyScalingAlarmPeriod} seconds`,
dimensions: [
{
name: 'LoadBalancerName',
value: loadBalancer.loadBalancerName,
},
],
evaluationPeriods: latencyScalingAlarmEvaluationPeriod,
metricName: 'Latency',
namespace: 'AWS/ELB',
period: latencyScalingAlarmPeriod,
statistic: 'Average',
threshold: latencyScalingAlarmThreshold,
comparisonOperator: 'GreaterThanOrEqualToThreshold',
okActions: [scaleDownPolicy.attrArn],
alarmActions: [scaleUpPolicy.attrArn],
});

const yamlTemplateFilePath = join(
__dirname,
'../..',
Expand Down
21 changes: 0 additions & 21 deletions dotcom-rendering/cloudformation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ Parameters:
Type: AWS::EC2::Image::Id

Conditions:
HasLatencyScalingAlarm: !Equals [!Ref Stage, 'PROD']
HasBackend5XXAlarm: !Equals [!Ref Stage, 'PROD']

Mappings:
Expand All @@ -84,26 +83,6 @@ Resources:
Roles:
- Ref: InstanceRole

LatencyScalingAlarm:
Condition: HasLatencyScalingAlarm
Properties:
AlarmDescription: !Sub |
Scale-Up if latency is greater than 0.2 seconds over 1 period(s) of 60 seconds
Dimensions:
- Name: LoadBalancerName
Value: !Ref InternalLoadBalancer
EvaluationPeriods: '1'
MetricName: Latency
Namespace: AWS/ELB
Period: '60'
Statistic: Average
Threshold: '0.2'
ComparisonOperator: GreaterThanOrEqualToThreshold
OKActions:
- !Ref ScaleDownPolicy
AlarmActions:
- !Ref ScaleUpPolicy
Type: AWS::CloudWatch::Alarm

Backend5xxAlarm:
Type: AWS::CloudWatch::Alarm
Expand Down