diff --git a/dotcom-rendering/cdk/lib/__snapshots__/dotcom-rendering.test.ts.snap b/dotcom-rendering/cdk/lib/__snapshots__/dotcom-rendering.test.ts.snap index 66008e1c05b..eeae7bcb8fa 100644 --- a/dotcom-rendering/cdk/lib/__snapshots__/dotcom-rendering.test.ts.snap +++ b/dotcom-rendering/cdk/lib/__snapshots__/dotcom-rendering.test.ts.snap @@ -12,14 +12,6 @@ exports[`The DotcomRendering stack matches the snapshot 1`] = ` "PROD", ], }, - "HasLatencyScalingAlarm": { - "Fn::Equals": [ - { - "Ref": "Stage", - }, - "PROD", - ], - }, }, "Description": "Frontend rendering service", "Mappings": { @@ -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": [ { @@ -872,7 +864,10 @@ exports[`The DotcomRendering stack matches the snapshot 1`] = ` "Namespace": "AWS/ELB", "OKActions": [ { - "Ref": "ScaleDownPolicy", + "Fn::GetAtt": [ + "ScaleDownPolicy", + "Arn", + ], }, ], "Period": 60, diff --git a/dotcom-rendering/cdk/lib/dotcom-rendering.ts b/dotcom-rendering/cdk/lib/dotcom-rendering.ts index ce2e2579a2b..71115214bc1 100644 --- a/dotcom-rendering/cdk/lib/dotcom-rendering.ts +++ b/dotcom-rendering/cdk/lib/dotcom-rendering.ts @@ -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'; @@ -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, '../..', diff --git a/dotcom-rendering/cloudformation.yml b/dotcom-rendering/cloudformation.yml index c2140ae137e..544ed7fdf0d 100644 --- a/dotcom-rendering/cloudformation.yml +++ b/dotcom-rendering/cloudformation.yml @@ -66,7 +66,6 @@ Parameters: Type: AWS::EC2::Image::Id Conditions: - HasLatencyScalingAlarm: !Equals [!Ref Stage, 'PROD'] HasBackend5XXAlarm: !Equals [!Ref Stage, 'PROD'] Mappings: @@ -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