|
1 | 1 | import { Lazy, Stack, Token } from '@aws-cdk/core';
|
2 | 2 | import { Construct } from 'constructs';
|
| 3 | +import { IAlarmAction } from './alarm-action'; |
3 | 4 | import { AlarmBase, IAlarm } from './alarm-base';
|
4 | 5 | import { CfnAlarm, CfnAlarmProps } from './cloudwatch.generated';
|
5 | 6 | import { HorizontalAnnotation } from './graph';
|
@@ -224,6 +225,33 @@ export class Alarm extends AlarmBase {
|
224 | 225 | return this.annotation;
|
225 | 226 | }
|
226 | 227 |
|
| 228 | + /** |
| 229 | + * Trigger this action if the alarm fires |
| 230 | + * |
| 231 | + * Typically the ARN of an SNS topic or ARN of an AutoScaling policy. |
| 232 | + */ |
| 233 | + public addAlarmAction(...actions: IAlarmAction[]) { |
| 234 | + if (this.alarmActionArns === undefined) { |
| 235 | + this.alarmActionArns = []; |
| 236 | + } |
| 237 | + |
| 238 | + this.alarmActionArns.push(...actions.map(a => |
| 239 | + this.validateActionArn(a.bind(this, this).alarmActionArn), |
| 240 | + )); |
| 241 | + } |
| 242 | + |
| 243 | + private validateActionArn(actionArn: string): string { |
| 244 | + const ec2ActionsRegexp: RegExp = /arn:aws:automate:[a-z|\d|-]+:ec2:[a-z]+/; |
| 245 | + if (ec2ActionsRegexp.test(actionArn)) { |
| 246 | + // Check per-instance metric |
| 247 | + const metricConfig = this.metric.toMetricConfig(); |
| 248 | + if (metricConfig.metricStat?.dimensions?.length != 1 || metricConfig.metricStat?.dimensions![0].name != 'InstanceId') { |
| 249 | + throw new Error(`EC2 alarm actions requires an EC2 Per-Instance Metric. (${JSON.stringify(metricConfig)} does not have an 'InstanceId' dimension)`); |
| 250 | + } |
| 251 | + } |
| 252 | + return actionArn; |
| 253 | + } |
| 254 | + |
227 | 255 | private renderMetric(metric: IMetric) {
|
228 | 256 | const self = this;
|
229 | 257 | return dispatchMetric(metric, {
|
|
0 commit comments