forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudwatch): EC2 actions (aws#13281)
Fixes aws#13228 ---- *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
1736d5b
commit 6dc2a2d
Showing
6 changed files
with
155 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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 EC2 actions available | ||
*/ | ||
export enum Ec2InstanceAction { | ||
/** | ||
* Stop the instance | ||
*/ | ||
STOP = 'stop', | ||
/** | ||
* Terminatethe instance | ||
*/ | ||
TERMINATE = 'terminate', | ||
/** | ||
* Recover the instance | ||
*/ | ||
RECOVER = 'recover', | ||
/** | ||
* Reboot the instance | ||
*/ | ||
REBOOT = 'reboot' | ||
} | ||
|
||
/** | ||
* Use an EC2 action as an Alarm action | ||
*/ | ||
export class Ec2Action implements cloudwatch.IAlarmAction { | ||
private ec2Action: Ec2InstanceAction; | ||
|
||
constructor(instanceAction: Ec2InstanceAction) { | ||
this.ec2Action = instanceAction; | ||
} | ||
|
||
/** | ||
* Returns an alarm action configuration to use an EC2 action as an alarm action | ||
*/ | ||
bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig { | ||
return { alarmActionArn: `arn:aws:automate:${Stack.of(_scope).region}:ec2:${this.ec2Action}` }; | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './appscaling'; | ||
export * from './autoscaling'; | ||
export * from './sns'; | ||
export * from './ec2'; |
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,41 @@ | ||
import '@aws-cdk/assert/jest'; | ||
import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; | ||
import { Stack } from '@aws-cdk/core'; | ||
import * as actions from '../lib'; | ||
|
||
test('can use instance reboot as alarm action', () => { | ||
// GIVEN | ||
const stack = new Stack(); | ||
const alarm = new cloudwatch.Alarm(stack, 'Alarm', { | ||
metric: new cloudwatch.Metric({ | ||
namespace: 'AWS/EC2', | ||
metricName: 'StatusCheckFailed', | ||
dimensions: { | ||
InstanceId: 'i-03cb889aaaafffeee', | ||
}, | ||
}), | ||
evaluationPeriods: 3, | ||
threshold: 100, | ||
}); | ||
|
||
// WHEN | ||
alarm.addAlarmAction(new actions.Ec2Action(actions.Ec2InstanceAction.REBOOT)); | ||
|
||
// THEN | ||
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', { | ||
AlarmActions: [ | ||
{ | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'arn:aws:automate:', | ||
{ | ||
Ref: 'AWS::Region', | ||
}, | ||
':ec2:reboot', | ||
], | ||
], | ||
}, | ||
], | ||
}); | ||
}); |
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