From a85da2577059f91b9e76a31834aef87622748937 Mon Sep 17 00:00:00 2001 From: Yeldos Balgabekov Date: Tue, 31 May 2022 13:36:18 +0200 Subject: [PATCH] (aws-cloudwatch-actions): Add the possibility to have SSM Incident Action This small PR will add SSM Incident action to cloudwatch alarm. The arn format was taken from the UI console (under Incident Manager Response Plan) --- .../@aws-cdk/aws-cloudwatch-actions/README.md | 12 ++++++++++ .../aws-cloudwatch-actions/lib/index.ts | 1 + .../lib/ssm-incidents.ts | 20 ++++++++++++++++ .../test/ssm-incidents.test.ts | 23 +++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 packages/@aws-cdk/aws-cloudwatch-actions/lib/ssm-incidents.ts create mode 100644 packages/@aws-cdk/aws-cloudwatch-actions/test/ssm-incidents.test.ts diff --git a/packages/@aws-cdk/aws-cloudwatch-actions/README.md b/packages/@aws-cdk/aws-cloudwatch-actions/README.md index 7788dc363a5ec..5aa5278feffa3 100644 --- a/packages/@aws-cdk/aws-cloudwatch-actions/README.md +++ b/packages/@aws-cdk/aws-cloudwatch-actions/README.md @@ -38,4 +38,16 @@ alarm.addAlarmAction( ); ``` +## SSM Incident Manager Action Example + +```ts +declare const alarm: cloudwatch.Alarm; +// Create an Incident Manager incident based on a specific response plan +alarm.addAlarmAction( + new actions.SsmIncidentAction( + 'arn:aws:ssm-incidents::123456789012:response-plan/ResponsePlanName' + ) +); +``` + See `@aws-cdk/aws-cloudwatch` for more information. diff --git a/packages/@aws-cdk/aws-cloudwatch-actions/lib/index.ts b/packages/@aws-cdk/aws-cloudwatch-actions/lib/index.ts index 191da008b23a9..ee3fa92684d29 100644 --- a/packages/@aws-cdk/aws-cloudwatch-actions/lib/index.ts +++ b/packages/@aws-cdk/aws-cloudwatch-actions/lib/index.ts @@ -3,3 +3,4 @@ export * from './autoscaling'; export * from './sns'; export * from './ec2'; export * from './ssm'; +export * from './ssm-incidents'; \ No newline at end of file diff --git a/packages/@aws-cdk/aws-cloudwatch-actions/lib/ssm-incidents.ts b/packages/@aws-cdk/aws-cloudwatch-actions/lib/ssm-incidents.ts new file mode 100644 index 0000000000000..9605fe1af8091 --- /dev/null +++ b/packages/@aws-cdk/aws-cloudwatch-actions/lib/ssm-incidents.ts @@ -0,0 +1,20 @@ +import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; +import { Construct } from '@aws-cdk/core'; + +/** + * Use an SSM Incident as an alarm action + */ +export class SsmIncidentAction implements cloudwatch.IAlarmAction { + constructor(private readonly responsePlanArn: string) { + } + + /** + * Returns an alarm action configuration to use an SSM Incident as an alarm action + * based on an Incident Manager Response Plan + */ + bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig { + return { + alarmActionArn: this.responsePlanArn, + }; + } +} diff --git a/packages/@aws-cdk/aws-cloudwatch-actions/test/ssm-incidents.test.ts b/packages/@aws-cdk/aws-cloudwatch-actions/test/ssm-incidents.test.ts new file mode 100644 index 0000000000000..25f8758e0ad28 --- /dev/null +++ b/packages/@aws-cdk/aws-cloudwatch-actions/test/ssm-incidents.test.ts @@ -0,0 +1,23 @@ +import { Template } from '@aws-cdk/assertions'; +import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; +import { Stack } from '@aws-cdk/core'; +import * as actions from '../lib'; + +test('can use SSM Incident as alarm action', () => { + // GIVEN + const stack = new Stack(); + const alarm = new cloudwatch.Alarm(stack, 'Alarm', { + metric: new cloudwatch.Metric({ namespace: 'AWS', metricName: 'Test' }), + evaluationPeriods: 3, + threshold: 100, + }); + + // WHEN + const responsePlanArn = 'arn:aws:ssm-incidents::123456789012:response-plan/ResponsePlanName'; + alarm.addAlarmAction(new actions.SsmIncidentAction(responsePlanArn)); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { + AlarmActions: [responsePlanArn], + }); +});