src/constructs/cloudwatch/alarm.ts (39 lines of code) (raw):

import { Alarm } from "aws-cdk-lib/aws-cloudwatch"; import type { AlarmProps } from "aws-cdk-lib/aws-cloudwatch"; import { SnsAction } from "aws-cdk-lib/aws-cloudwatch-actions"; import { Topic } from "aws-cdk-lib/aws-sns"; import type { ITopic } from "aws-cdk-lib/aws-sns"; import type { AppIdentity, GuStack } from "../core"; export interface GuAlarmProps extends AlarmProps, AppIdentity { snsTopicName: string; okAction?: boolean; } export interface Http4xxAlarmProps extends Omit< GuAlarmProps, "snsTopicName" | "evaluationPeriods" | "metric" | "period" | "threshold" | "treatMissingData" | "app" > { tolerated4xxPercentage: number; numberOfMinutesAboveThresholdBeforeAlarm?: number; } export interface Http5xxAlarmProps extends Omit< GuAlarmProps, "snsTopicName" | "evaluationPeriods" | "metric" | "period" | "threshold" | "treatMissingData" | "app" > { tolerated5xxPercentage: number; numberOfMinutesAboveThresholdBeforeAlarm?: number; } /** * Creates a CloudWatch alarm which sends notifications to the specified SNS topic. * * Alarm actions are enabled by default. * * To enable the alarm only in PROD, use the value of `Stage`: * ```typescript * new GuAlarm(stack, "alarm", { * // other required props * actionsEnabled: this.stage === "PROD", * }); * ``` * * This library provides an implementation of some commonly used alarms, which require less boilerplate than this construct, * for example the [[`GuAlb5xxPercentageAlarm`]]. Prefer using these more specific implementations where possible. */ export class GuAlarm extends Alarm { constructor(scope: GuStack, id: string, props: GuAlarmProps) { const { region, account } = scope; const { snsTopicName, actionsEnabled = true, okAction } = props; super(scope, id, { ...props, actionsEnabled }); const topicArn: string = `arn:aws:sns:${region}:${account}:${snsTopicName}`; const snsTopic: ITopic = Topic.fromTopicArn(scope, `SnsTopicFor${id}`, topicArn); this.addAlarmAction(new SnsAction(snsTopic)); if (okAction) { this.addOkAction(new SnsAction(snsTopic)); } } }