Skip to content

Commit

Permalink
(aws-cloudwatch-actions): Add the possibility to have SSM Incident Ac…
Browse files Browse the repository at this point in the history
…tion

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)
  • Loading branch information
aws-byeldos committed May 31, 2022
1 parent d814293 commit a85da25
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-cloudwatch-actions/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './autoscaling';
export * from './sns';
export * from './ec2';
export * from './ssm';
export * from './ssm-incidents';
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch-actions/lib/ssm-incidents.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
}
Original file line number Diff line number Diff line change
@@ -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],
});
});

0 comments on commit a85da25

Please sign in to comment.