diff --git a/cdk/lib/app-stack.ts b/cdk/lib/app-stack.ts index 3fb73c70..1817c9d7 100644 --- a/cdk/lib/app-stack.ts +++ b/cdk/lib/app-stack.ts @@ -6,9 +6,12 @@ import type { Construct } from "constructs"; import * as ecs from "aws-cdk-lib/aws-ecs"; import * as ecsPatterns from "aws-cdk-lib/aws-ecs-patterns"; import * as ecrAssets from "aws-cdk-lib/aws-ecr-assets"; +import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch"; import * as logs from "aws-cdk-lib/aws-logs"; import * as ssm from "aws-cdk-lib/aws-ssm"; +import * as sns from "aws-cdk-lib/aws-sns"; import * as iam from "aws-cdk-lib/aws-iam"; +import * as snsSubscriptions from "aws-cdk-lib/aws-sns-subscriptions"; interface Props extends cdk.StackProps { bucket: cdk.aws_s3.Bucket; @@ -202,5 +205,29 @@ export class AppStack extends cdk.Stack { ); this.loadbalancer = fargateService.loadBalancer; + + // Cloudwatch notifications + const alarm = new cloudwatch.Alarm(this, "FargateServiceDownAlarm", { + alarmName: "FargateServiceDownAlarm", + alarmDescription: "Triggered when Fargate service is down", + metric: fargateService.service.metricCpuUtilization(), + threshold: 10, + evaluationPeriods: 1, + comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_THRESHOLD, + treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING, + }); + + const snsTopic = new sns.Topic(this, "FargateServiceAlertTopic"); + alarm.addAlarmAction(new cdk.aws_cloudwatch_actions.SnsAction(snsTopic)); + + snsTopic.addSubscription( + new snsSubscriptions.EmailSubscription( + ssm.StringParameter.fromStringParameterName( + this, + "alertEmail", + "/env/alertEmail", + ).toString(), + ), + ); } } diff --git a/cdk/test/cdk-pipeline-codu.test.ts b/cdk/test/cdk-pipeline-codu.test.ts deleted file mode 100644 index 8e74cdd0..00000000 --- a/cdk/test/cdk-pipeline-codu.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -// import * as cdk from 'aws-cdk-lib'; -// import { Template } from 'aws-cdk-lib/assertions'; -// import * as CdkPipelineDocker from '../lib/cdk-pipeline-docker-stack'; - -// example test. To run these tests, uncomment this file along with the -// example resource in lib/cdk-pipeline-docker-stack.ts -test("SQS Queue Created", () => { - // const app = new cdk.App(); - // // WHEN - // const stack = new CdkPipelineDocker.CdkPipelineDockerStack(app, 'MyTestStack'); - // // THEN - // const template = Template.fromStack(stack); - // template.hasResourceProperties('AWS::SQS::Queue', { - // VisibilityTimeout: 300 - // }); -});