1- import { join } from 'path' ;
2- import type { GuStackProps } from '@guardian/cdk/lib/constructs/core' ;
3- import { GuStack } from '@guardian/cdk/lib/constructs/core' ;
4- import type { App } from 'aws-cdk-lib' ;
5- import { CfnInclude } from 'aws-cdk-lib/cloudformation-include' ;
1+ import { join } from 'path' ;
2+ import type { GuStackProps } from '@guardian/cdk/lib/constructs/core' ;
3+ import { GuStack } from '@guardian/cdk/lib/constructs/core' ;
4+ import type { App } from 'aws-cdk-lib' ;
5+ import { CfnInclude } from 'aws-cdk-lib/cloudformation-include' ;
6+ import { GuEc2App } from "@guardian/cdk" ;
7+ import { InstanceClass , InstanceSize , InstanceType } from "aws-cdk-lib/aws-ec2" ;
8+ import { AccessScope } from "@guardian/cdk/lib/constants" ;
9+ import { HttpCodeTarget } from "aws-cdk-lib/aws-elasticloadbalancingv2" ;
10+
11+ export interface RegistrationProps extends GuStackProps {
12+ instanceMetricGranularity : '1Minute' | '5Minute' ;
13+ minAsgSize : number ;
14+ maxAsgSize : number ;
15+ }
616
717export class Registration extends GuStack {
8- constructor ( scope : App , id : string , props : GuStackProps ) {
18+ constructor ( scope : App , id : string , props : RegistrationProps ) {
919 super ( scope , id , props ) ;
1020 const yamlTemplateFilePath = join (
1121 __dirname ,
@@ -17,5 +27,42 @@ export class Registration extends GuStack {
1727 new CfnInclude ( this , 'YamlTemplate' , {
1828 templateFile : yamlTemplateFilePath ,
1929 } ) ;
30+
31+ const app = "registration" ;
32+
33+ const { instanceMetricGranularity, minAsgSize, maxAsgSize} = props ;
34+
35+ const ec2App = new GuEc2App ( this , {
36+ access : {
37+ scope : AccessScope . PUBLIC ,
38+ } ,
39+ app : app ,
40+ applicationPort : 9000 ,
41+ instanceMetricGranularity : instanceMetricGranularity ,
42+ instanceType : InstanceType . of ( InstanceClass . T4G , InstanceSize . SMALL ) ,
43+ // This matches the YAML stack (i.e. there is no 5XX alarm).
44+ monitoringConfiguration : { noMonitoring : true } ,
45+ scaling : { minimumInstances : minAsgSize , maximumInstances : maxAsgSize } ,
46+ userData : {
47+ distributable : {
48+ fileName : `${ app } _1.0-latest_all.deb` ,
49+ executionStatement : `dpkg -i /report/${ app } _1.0-latest_all.deb` ,
50+ } ,
51+ }
52+ } ) ;
53+
54+ ec2App . autoScalingGroup . scaleOnCpuUtilization ( "CpuScalingPolicy" , {
55+ targetUtilizationPercent : 20 ,
56+ } ) ;
57+
58+ const alarm2xxMetric = ec2App . loadBalancer . metrics . httpCodeTarget (
59+ HttpCodeTarget . TARGET_2XX_COUNT )
60+
61+ alarm2xxMetric . createAlarm ( this , '2XXAlarm-per-hour' , {
62+ evaluationPeriods : 12 ,
63+ threshold : 1000 ,
64+ } )
65+
66+
2067 }
2168}
0 commit comments