Menu
AWS SDK for JavaScript
Developer Guide for SDK version 2.122.0

Creating Alarms in Amazon CloudWatch


                      Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to:

  • Retrieve basic information about your CloudWatch alarms.

  • Create and delete a CloudWatch alarm.

The Scenario

An alarm watches a single metric over a time period you specify, and performs one or more actions based on the value of the metric relative to a given threshold over a number of time periods.

In this example, a series of Node.js modules are used to create alarms in CloudWatch. The Node.js modules use the SDK for JavaScript to create alarms using these methods of the AWS.CloudWatch client class:

For more information about CloudWatch alarms, see Creating Amazon CloudWatch Alarms in the Amazon CloudWatch User Guide.

Prerequisite Tasks

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

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object, setting the region, and providing credentials for your code. In this example, the credentials are provided using the JSON file you created.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json');

Describing Alarms

Create a Node.js module with the file name cw_describealarms.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 retrieving alarm descriptions, limiting the alarms returned to those with a state of INSUFFICIENT_DATA. Then call the describeAlarms method of the AWS.CloudWatch service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); cw.describeAlarms({StateValue: 'INSUFFICIENT_DATA'}, function(err, data) { if (err) { console.log("Error", err); } else { // List the names of all current alarms in the console data.MetricAlarms.forEach(function (item, index, array) { console.log(item.AlarmName); }); } });

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

Copy
node cw_describealarms.js

This sample code can be found here on GitHub.

Creating an Alarm for a CloudWatch Metric

Create a Node.js module with the file name cw_putmetricalarm.js. Be sure to configure the SDK as previously shown. To access CloudWatch, create an AWS.CloudWatch service object. Create a JSON object for the parameters needed to create an alarm based on a metric, in this case the CPU utilization of an Amazon EC2 instance. The remaining parameters are set so the alarm triggers when the metric exceeds a threshold of 70 percent. Then call the describeAlarms method of the AWS.CloudWatch service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // 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: false, 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("Success", data); } });

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

Copy
node cw_putmetricalarm.js

This sample code can be found here on GitHub.

Deleting an Alarm

Create a Node.js module with the file name cw_deletealarms.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 names of the alarms you want to delete. Then call the deleteAlarms method of the AWS.CloudWatch service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); cw.deleteAlarms({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_deletealarms.js

This sample code can be found here on GitHub.