Skip to content

Commit 9380885

Browse files
authored
feat(cloudwatch-actions): add ssm opsitem action for cloudwatch alarm (#16923)
This small PR will add SSM OpsItem action to cloudwatch alarm. The arn format was taken from the alarm UI (in view source) and with all the parameters (severity and category) closes #16861 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a2eb092 commit 9380885

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

packages/@aws-cdk/aws-cloudwatch-actions/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
This library contains a set of classes which can be used as CloudWatch Alarm actions.
1313

14-
The currently implemented actions are: EC2 Actions, SNS Actions, Autoscaling Actions and Aplication Autoscaling Actions
14+
The currently implemented actions are: EC2 Actions, SNS Actions, SSM OpsCenter Actions, Autoscaling Actions and Application Autoscaling Actions
1515

1616

1717
## EC2 Action Example
@@ -25,4 +25,17 @@ alarm.addAlarmAction(
2525
);
2626
```
2727

28+
## SSM OpsCenter Action Example
29+
30+
```ts
31+
declare const alarm: cloudwatch.Alarm;
32+
// Create an OpsItem with specific severity and category when alarm triggers
33+
alarm.addAlarmAction(
34+
new actions.SsmAction(
35+
actions.OpsItemSeverity.CRITICAL,
36+
actions.OpsItemCategory.PERFORMANCE // category is optional
37+
)
38+
);
39+
```
40+
2841
See `@aws-cdk/aws-cloudwatch` for more information.

packages/@aws-cdk/aws-cloudwatch-actions/lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './appscaling';
22
export * from './autoscaling';
33
export * from './sns';
44
export * from './ec2';
5+
export * from './ssm';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
2+
import { Stack } from '@aws-cdk/core';
3+
4+
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
5+
// eslint-disable-next-line no-duplicate-imports, import/order
6+
import { Construct } from '@aws-cdk/core';
7+
8+
/**
9+
* Types of OpsItem severity available
10+
*/
11+
export enum OpsItemSeverity {
12+
/**
13+
* Set the severity to critical
14+
*/
15+
CRITICAL = '1',
16+
/**
17+
* Set the severity to high
18+
*/
19+
HIGH = '2',
20+
/**
21+
* Set the severity to medium
22+
*/
23+
MEDIUM = '3',
24+
/**
25+
* Set the severity to low
26+
*/
27+
LOW = '4'
28+
}
29+
30+
/**
31+
* Types of OpsItem category available
32+
*/
33+
export enum OpsItemCategory {
34+
/**
35+
* Set the category to availability
36+
*/
37+
AVAILABILITY = 'Availability',
38+
/**
39+
* Set the category to cost
40+
*/
41+
COST = 'Cost',
42+
/**
43+
* Set the category to performance
44+
*/
45+
PERFORMANCE = 'Performance',
46+
/**
47+
* Set the category to recovery
48+
*/
49+
RECOVERY = 'Recovery',
50+
/**
51+
* Set the category to security
52+
*/
53+
SECURITY = 'Security'
54+
}
55+
56+
/**
57+
* Use an SSM OpsItem action as an Alarm action
58+
*/
59+
export class SsmAction implements cloudwatch.IAlarmAction {
60+
private severity: OpsItemSeverity;
61+
private category?: OpsItemCategory;
62+
63+
constructor(severity: OpsItemSeverity, category?: OpsItemCategory) {
64+
this.severity = severity;
65+
this.category = category;
66+
}
67+
68+
/**
69+
* Returns an alarm action configuration to use an SSM OpsItem action as an alarm action
70+
*/
71+
bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
72+
if (this.category === undefined) {
73+
return { alarmActionArn: `arn:aws:ssm:${Stack.of(_scope).region}:${Stack.of(_scope).account}:opsitem:${this.severity}` };
74+
} else {
75+
return { alarmActionArn: `arn:aws:ssm:${Stack.of(_scope).region}:${Stack.of(_scope).account}:opsitem:${this.severity}#CATEGORY=${this.category}` };
76+
}
77+
}
78+
}
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Template } from '@aws-cdk/assertions';
2+
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
3+
import { Stack } from '@aws-cdk/core';
4+
import * as actions from '../lib';
5+
6+
test('can use ssm with critical severity and performance category as alarm action', () => {
7+
// GIVEN
8+
const stack = new Stack();
9+
const alarm = new cloudwatch.Alarm(stack, 'Alarm', {
10+
metric: new cloudwatch.Metric({
11+
namespace: 'AWS',
12+
metricName: 'Test',
13+
}),
14+
evaluationPeriods: 3,
15+
threshold: 100,
16+
});
17+
18+
// WHEN
19+
alarm.addAlarmAction(new actions.SsmAction(actions.OpsItemSeverity.CRITICAL, actions.OpsItemCategory.PERFORMANCE));
20+
21+
// THEN
22+
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
23+
AlarmActions: [
24+
{
25+
'Fn::Join': [
26+
'',
27+
[
28+
'arn:aws:ssm:',
29+
{
30+
Ref: 'AWS::Region',
31+
},
32+
':',
33+
{
34+
Ref: 'AWS::AccountId',
35+
},
36+
':opsitem:1#CATEGORY=Performance',
37+
],
38+
],
39+
},
40+
],
41+
});
42+
});
43+
44+
45+
test('can use ssm with meduim severity and no category as alarm action', () => {
46+
// GIVEN
47+
const stack = new Stack();
48+
const alarm = new cloudwatch.Alarm(stack, 'Alarm', {
49+
metric: new cloudwatch.Metric({
50+
namespace: 'AWS',
51+
metricName: 'Test',
52+
}),
53+
evaluationPeriods: 3,
54+
threshold: 100,
55+
});
56+
57+
// WHEN
58+
alarm.addAlarmAction(new actions.SsmAction(actions.OpsItemSeverity.MEDIUM));
59+
60+
// THEN
61+
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
62+
AlarmActions: [
63+
{
64+
'Fn::Join': [
65+
'',
66+
[
67+
'arn:aws:ssm:',
68+
{
69+
Ref: 'AWS::Region',
70+
},
71+
':',
72+
{
73+
Ref: 'AWS::AccountId',
74+
},
75+
':opsitem:3',
76+
],
77+
],
78+
},
79+
],
80+
});
81+
});
82+

0 commit comments

Comments
 (0)