|
| 1 | +import {Duration} from "aws-cdk-lib" |
| 2 | +import {Construct} from "constructs" |
| 3 | +import { |
| 4 | + Alarm, |
| 5 | + ComparisonOperator, |
| 6 | + CreateAlarmOptions, |
| 7 | + Metric, |
| 8 | + MetricStatConfig, |
| 9 | + TreatMissingData, |
| 10 | + Unit |
| 11 | +} from "aws-cdk-lib/aws-cloudwatch" |
| 12 | +import {SnsAction} from "aws-cdk-lib/aws-cloudwatch-actions" |
| 13 | +import {ITopic} from "aws-cdk-lib/aws-sns" |
| 14 | + |
| 15 | +/** |
| 16 | + * Alarm definition for SnsAlarm with defaults applied by the construct. |
| 17 | + */ |
| 18 | +export interface SnsAlarmDefinition |
| 19 | + extends Omit<CreateAlarmOptions, "threshold" | "evaluationPeriods"> { |
| 20 | + /** |
| 21 | + * The value against which the specified statistic is compared. |
| 22 | + * |
| 23 | + * @default 1 |
| 24 | + */ |
| 25 | + readonly threshold?: number |
| 26 | + |
| 27 | + /** |
| 28 | + * The number of periods over which data is compared to the specified threshold. |
| 29 | + * |
| 30 | + * @default 1 |
| 31 | + */ |
| 32 | + readonly evaluationPeriods?: number |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Metric stat configuration for SnsAlarm with defaults applied by the construct. |
| 37 | + */ |
| 38 | +export interface SnsMetricStatConfig extends Omit<MetricStatConfig, "period" | "statistic"> { |
| 39 | + /** |
| 40 | + * How many seconds to aggregate over. |
| 41 | + * |
| 42 | + * @default Duration.minutes(1) |
| 43 | + */ |
| 44 | + readonly period?: Duration |
| 45 | + |
| 46 | + /** |
| 47 | + * Aggregation function to use. |
| 48 | + * |
| 49 | + * @default "Sum" |
| 50 | + */ |
| 51 | + readonly statistic?: string |
| 52 | +} |
| 53 | + |
| 54 | +const toDimensionsMap = ( |
| 55 | + dimensions: MetricStatConfig["dimensions"] |
| 56 | +): {[dimensionName: string]: string} | undefined => { |
| 57 | + if (!dimensions || dimensions.length === 0) { |
| 58 | + return undefined |
| 59 | + } |
| 60 | + |
| 61 | + const dimensionMap: {[dimensionName: string]: string} = {} |
| 62 | + dimensions.forEach((dimension) => { |
| 63 | + dimensionMap[dimension.name] = String(dimension.value) |
| 64 | + }) |
| 65 | + return dimensionMap |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Constructs a concrete CloudWatch Metric from a MetricStatConfig. |
| 70 | + * @see {@link import("aws-cdk-lib/aws-cloudwatch").MetricConfig} for alternate concrete metric configs. |
| 71 | + */ |
| 72 | +export const metricFromStatConfig = ( |
| 73 | + metricStatConfig: MetricStatConfig |
| 74 | +): Metric => |
| 75 | + new Metric({ |
| 76 | + namespace: metricStatConfig.namespace, |
| 77 | + metricName: metricStatConfig.metricName, |
| 78 | + dimensionsMap: toDimensionsMap(metricStatConfig.dimensions), |
| 79 | + statistic: metricStatConfig.statistic, |
| 80 | + period: metricStatConfig.period, |
| 81 | + unit: metricStatConfig.unitFilter, |
| 82 | + account: metricStatConfig.accountOverride ?? metricStatConfig.account, |
| 83 | + region: metricStatConfig.regionOverride ?? metricStatConfig.region |
| 84 | + }) |
| 85 | + |
| 86 | +/** |
| 87 | + * Configuration for creating a CloudWatch metric alarm with SNS publication construct. |
| 88 | + */ |
| 89 | +export interface SnsAlarmProps { |
| 90 | + |
| 91 | + /** Prefix used in the generated CloudWatch alarm name. */ |
| 92 | + readonly stackName: string |
| 93 | + /** Enables alarm actions when true, disabling notifications when false. */ |
| 94 | + readonly enableAlerts: boolean |
| 95 | + /** CloudWatch metric and threshold settings for the alarm. */ |
| 96 | + readonly alarmDefinition: SnsAlarmDefinition |
| 97 | + /** Defines the metric configuration to be monitored by the alarm. */ |
| 98 | + readonly metricStatConfig: SnsMetricStatConfig |
| 99 | + /** SNS topic that receives alarm, OK, and insufficient data notifications. Common example is for Slack alerts. */ |
| 100 | + readonly snsTopic: ITopic |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Creates a single CloudWatch alarm and wires all alarm state changes to an SNS topic. |
| 105 | + */ |
| 106 | +export class SnsAlarm extends Construct { |
| 107 | + public readonly alarm: Alarm |
| 108 | + |
| 109 | + /** |
| 110 | + * Creates a CloudWatch alarm and publishes alarm state changes to the provided SNS topic. |
| 111 | + * |
| 112 | + * @param props Alarm configuration including metric settings, threshold settings, and notification topic. |
| 113 | + * @example |
| 114 | + * new SnsAlarm(this, 'MyApiErrorAlarm', { |
| 115 | + * stackName: 'pfp-prod', |
| 116 | + * enableAlerts: true, |
| 117 | + * alarmDefinition: { |
| 118 | + * alarmDescription: 'API errors detected', |
| 119 | + * threshold: 1 |
| 120 | + * }, |
| 121 | + * metricStatConfig: { |
| 122 | + * namespace: 'LambdaLogFilterMetrics', |
| 123 | + * metricName: 'ErrorCount' |
| 124 | + * }, |
| 125 | + * snsTopic: slackAlertTopic |
| 126 | + * }) |
| 127 | + */ |
| 128 | + public constructor(scope: Construct, id: string, props: SnsAlarmProps) { |
| 129 | + super(scope, id) |
| 130 | + |
| 131 | + const generatedAlarmName = props.alarmDefinition.alarmName ?? id |
| 132 | + const { |
| 133 | + threshold, |
| 134 | + evaluationPeriods, |
| 135 | + comparisonOperator, |
| 136 | + treatMissingData, |
| 137 | + ...supportedAlarmDefinitionProps |
| 138 | + } = props.alarmDefinition |
| 139 | + |
| 140 | + const alarm = new Alarm(this, `${generatedAlarmName}Alarm`, { |
| 141 | + ...supportedAlarmDefinitionProps, |
| 142 | + alarmName: `${props.stackName}-${generatedAlarmName}`, |
| 143 | + metric: metricFromStatConfig({ |
| 144 | + ...props.metricStatConfig, |
| 145 | + unitFilter: props.metricStatConfig.unitFilter ?? Unit.COUNT, |
| 146 | + statistic: props.metricStatConfig.statistic ?? "Sum", |
| 147 | + period: props.metricStatConfig.period ?? Duration.minutes(1) |
| 148 | + }), |
| 149 | + threshold: threshold ?? 1, |
| 150 | + evaluationPeriods: evaluationPeriods ?? 1, |
| 151 | + comparisonOperator: comparisonOperator ?? ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD, |
| 152 | + treatMissingData: treatMissingData ?? TreatMissingData.NOT_BREACHING, |
| 153 | + actionsEnabled: props.enableAlerts |
| 154 | + }) |
| 155 | + |
| 156 | + const snsAction = new SnsAction(props.snsTopic) |
| 157 | + alarm.addAlarmAction(snsAction) |
| 158 | + alarm.addOkAction(snsAction) |
| 159 | + alarm.addInsufficientDataAction(snsAction) |
| 160 | + |
| 161 | + this.alarm = alarm |
| 162 | + } |
| 163 | +} |
0 commit comments