Creating Alarms in Amazon CloudWatch

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:

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.

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.

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.

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.