Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudwatch): EC2 actions #13281

Merged
merged 9 commits into from Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-cloudwatch-actions/README.md
Expand Up @@ -11,4 +11,5 @@

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

The currently implemented actions are: EC2 Actions, SNS Actions, Autoscaling Actions and Aplication Autoscaling Actions
See `@aws-cdk/aws-cloudwatch` for more information.
47 changes: 47 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch-actions/lib/ec2.ts
@@ -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 Ec2InstanceActions {
cyuste marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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: string;
cyuste marked this conversation as resolved.
Show resolved Hide resolved

constructor(instanceAction: Ec2InstanceActions) {
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}` };
cyuste marked this conversation as resolved.
Show resolved Hide resolved
}
}

1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-cloudwatch-actions/lib/index.ts
@@ -1,3 +1,4 @@
export * from './appscaling';
export * from './autoscaling';
export * from './sns';
export * from './ec2';
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch-actions/test/ec2.test.ts
@@ -0,0 +1,36 @@
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 topic = new sns.Topic(stack, 'Topic');
const alarm = new cloudwatch.Alarm(stack, 'Alarm', {
metric: new cloudwatch.Metric({ namespace: 'AWS', metricName: 'Henk' }),
evaluationPeriods: 3,
threshold: 100,
});

// WHEN
alarm.addAlarmAction(new actions.Ec2Action(actions.Ec2InstanceActions.REBOOT));

// THEN
expect(stack).toHaveResource('AWS::CloudWatch::Alarm', {
AlarmActions: [
{
'Fn::Join': [
'',
[
'arn:aws:automate:',
{
Ref: 'AWS::Region',
},
':ec2:reboot',
],
],
},
],
});
});