-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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*
- Loading branch information
1 parent
a2eb092
commit 9380885
Showing
4 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; | ||
import { Stack } from '@aws-cdk/core'; | ||
|
||
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main | ||
// eslint-disable-next-line no-duplicate-imports, import/order | ||
import { Construct } from '@aws-cdk/core'; | ||
|
||
/** | ||
* Types of OpsItem severity available | ||
*/ | ||
export enum OpsItemSeverity { | ||
/** | ||
* Set the severity to critical | ||
*/ | ||
CRITICAL = '1', | ||
/** | ||
* Set the severity to high | ||
*/ | ||
HIGH = '2', | ||
/** | ||
* Set the severity to medium | ||
*/ | ||
MEDIUM = '3', | ||
/** | ||
* Set the severity to low | ||
*/ | ||
LOW = '4' | ||
} | ||
|
||
/** | ||
* Types of OpsItem category available | ||
*/ | ||
export enum OpsItemCategory { | ||
/** | ||
* Set the category to availability | ||
*/ | ||
AVAILABILITY = 'Availability', | ||
/** | ||
* Set the category to cost | ||
*/ | ||
COST = 'Cost', | ||
/** | ||
* Set the category to performance | ||
*/ | ||
PERFORMANCE = 'Performance', | ||
/** | ||
* Set the category to recovery | ||
*/ | ||
RECOVERY = 'Recovery', | ||
/** | ||
* Set the category to security | ||
*/ | ||
SECURITY = 'Security' | ||
} | ||
|
||
/** | ||
* Use an SSM OpsItem action as an Alarm action | ||
*/ | ||
export class SsmAction implements cloudwatch.IAlarmAction { | ||
private severity: OpsItemSeverity; | ||
private category?: OpsItemCategory; | ||
|
||
constructor(severity: OpsItemSeverity, category?: OpsItemCategory) { | ||
this.severity = severity; | ||
this.category = category; | ||
} | ||
|
||
/** | ||
* Returns an alarm action configuration to use an SSM OpsItem action as an alarm action | ||
*/ | ||
bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig { | ||
if (this.category === undefined) { | ||
return { alarmActionArn: `arn:aws:ssm:${Stack.of(_scope).region}:${Stack.of(_scope).account}:opsitem:${this.severity}` }; | ||
} else { | ||
return { alarmActionArn: `arn:aws:ssm:${Stack.of(_scope).region}:${Stack.of(_scope).account}:opsitem:${this.severity}#CATEGORY=${this.category}` }; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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 with critical severity and performance category 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 | ||
alarm.addAlarmAction(new actions.SsmAction(actions.OpsItemSeverity.CRITICAL, actions.OpsItemCategory.PERFORMANCE)); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { | ||
AlarmActions: [ | ||
{ | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'arn:aws:ssm:', | ||
{ | ||
Ref: 'AWS::Region', | ||
}, | ||
':', | ||
{ | ||
Ref: 'AWS::AccountId', | ||
}, | ||
':opsitem:1#CATEGORY=Performance', | ||
], | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
|
||
test('can use ssm with meduim severity and no category 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 | ||
alarm.addAlarmAction(new actions.SsmAction(actions.OpsItemSeverity.MEDIUM)); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { | ||
AlarmActions: [ | ||
{ | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'arn:aws:ssm:', | ||
{ | ||
Ref: 'AWS::Region', | ||
}, | ||
':', | ||
{ | ||
Ref: 'AWS::AccountId', | ||
}, | ||
':opsitem:3', | ||
], | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|