Menu
AWS SDK for JavaScript
Developer Guide for SDK version 2.119.0

Getting Metrics from Amazon CloudWatch


                      Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to:

  • Retrieve a list of published CloudWatch metrics.

  • Publish data points to CloudWatch metrics.

The Scenario

Metrics are data about the performance of your systems. You can enable detailed monitoring of some resources, such as your Amazon EC2 instances, or your own application metrics.

In this example, a series of Node.js modules are used to get metrics from CloudWatch. In this example, a series of Node.js modules are used to send events to Amazon CloudWatch Events. The Node.js modules use the SDK for JavaScript to get metrics from CloudWatch using these methods of the CloudWatch client class:

For more information about CloudWatch metrics, see Using Amazon CloudWatch Metrics 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');

Listing Metrics

Create a Node.js module with the file name cw_listmetrics.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 parameters needed to list metrics within the AWS/Logs namespace. Call the listMetrics method to list the IncomingLogEvents metric.

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 = { Dimensions: [ { Name: 'LogGroupName', }, ], MetricName: 'IncomingLogEvents', Namespace: 'AWS/Logs' }; cw.listMetrics(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Metrics", JSON.stringify(data.Metrics)); } });

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

Copy
node cw_listmetrics.js

This sample code can be found here on GitHub.

Submitting Custom Metrics

Create a Node.js module with the file name cw_putmetricdata.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 parameters needed to submit a data point for the PAGES_VISITED custom metric. Call the putMetricData method.

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'}); // Create parameters JSON for putMetricData var params = { MetricData: [ { MetricName: 'PAGES_VISITED', Dimensions: [ { Name: 'UNIQUE_PAGES', Value: 'URLS' }, ], Unit: 'None', Value: 1.0 }, ], Namespace: 'SITE/TRAFFIC' }; cw.putMetricData(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_putmetricdata.js

This sample code can be found here on GitHub.