Menu
AWS SDK for JavaScript
Developer Guide for SDK v2.154.0

Using Alarm Actions in Amazon CloudWatch

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to change the state of your Amazon EC2 instances automatically based on a CloudWatch alarm.

The Scenario

Using alarm actions, you can create alarms that automatically stop, terminate, reboot, or recover your Amazon EC2 instances. You can use the stop or terminate actions when you no longer need an instance to be running. You can use the reboot and recover actions to automatically reboot those instances.

In this example, a series of Node.js modules are used to define an alarm action in CloudWatch that triggers the reboot of an Amazon EC2 instance. The Node.js modules use the SDK for JavaScript to manage Amazon EC2 instances using these methods of the CloudWatch client class:

For more information about CloudWatch alarm actions, see Create Alarms to Stop, Terminate, Reboot, or Recover an Instance in the Amazon CloudWatch User Guide.

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Use the following role policy when creating the IAM role.

Copy
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "cloudwatch:Describe*", "ec2:Describe*", "ec2:RebootInstances", "ec2:StopInstances*", "ec2:TerminateInstances" ], "Resource": [ "*" ] } ] }

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-west-2'});

Creating and Enabling Actions on an Alarm

Create a Node.js module with the file name cw_enablealarmactions.js. Be sure to configure the SDK as previously shown. To access CloudWatch, create an AWS.CloudWatch service object.

Create a JSON object to hold the parameters for creating an alarm, specifying ActionsEnabled as true and an array of ARNs for the actions the alarm will trigger. Call the putMetricAlarm method of the AWS.CloudWatch service object, which creates the alarm if it does not exist or updates it if the alarm does exist.

In the callback function for the putMetricAlarm, upon successful completion create a JSON object containing the name of the CloudWatch alarm. Call the enableAlarmActions method to enable the alarm action.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); var params = { AlarmName: 'Web_Server_CPU_Utilization', ComparisonOperator: 'GreaterThanThreshold', EvaluationPeriods: 1, MetricName: 'CPUUtilization', Namespace: 'AWS/EC2', Period: 60, Statistic: 'Average', Threshold: 70.0, ActionsEnabled: true, AlarmActions: ['arn:aws:swf:us-west-2:{CUSTOMER_ACCOUNT}:action/actions/AWS_EC2.InstanceId.Reboot/1.0'], /* This is a workflow */ AlarmDescription: 'Alarm when server CPU exceeds 70%', Dimensions: [ { Name: 'InstanceId', Value: 'INSTANCE_ID' }, ], Unit: 'Seconds' }; cw.putMetricAlarm(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Alarm action added", data); var paramsEnableAlarmAction = { AlarmNames: [paramsUpdateAlarm.AlarmName] }; cw.enableAlarmActions(paramsEnableAlarmAction, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Alarm action enabled", data); } }); } });

To run the example, type the following at the command line.

Copy
node cw_enablealarmactions.js

This sample code can be found here on GitHub.

Disabling Actions on an Alarm

Create a Node.js module with the file name cw_disablealarmactions.js. Be sure to configure the SDK as previously shown. To access CloudWatch, create an AWS.CloudWatch service object. Create a JSON object containing the name of the CloudWatch alarm. Call the disableAlarmActions method to disable the actions for this alarm.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); cw.disableAlarmActions({AlarmNames: ['Web_Server_CPU_Utilization']}, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

To run the example, type the following at the command line.

Copy
node cw_disablealarmactions.js

This sample code can be found here on GitHub.