Menu
AWS SDK for JavaScript
Developer Guide for SDK version 2.120.0

Sending Events to Amazon CloudWatch Events


                      Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to:

  • Create and update a rule used to trigger an event.

  • Define one or more targets to respond to an event.

  • Send events that are matched to targets for handling.

The Scenario

CloudWatch Events delivers a near real-time stream of system events that describe changes in Amazon Web Services (AWS) resources to any of various targets. Using simple rules, you can match events and route them to one or more target functions or streams.

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

For more information about CloudWatch Events, see Adding Events with PutEvents in the Amazon CloudWatch Events 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": [ { "Sid": "CloudWatchEventsFullAccess", "Effect": "Allow", "Action": "events:*", "Resource": "*" }, { "Sid": "IAMPassRoleForCloudWatchEvents", "Effect": "Allow", "Action": "iam:PassRole", "Resource": "arn:aws:iam::*:role/AWS_Events_Invoke_Targets" } ] }

Use the following trust relationship when creating the IAM role.

Copy
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }

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');

Creating a Scheduled Rule

Create a Node.js module with the file name cwe_putrule.js. Be sure to configure the SDK as previously shown. To access CloudWatch Events, create an AWS.CloudWatchEvents service object. Create a JSON object containing the parameters needed to specify the new scheduled rule, which include the following:

  • A name for the rule

  • The ARN of the IAM role you created previously

  • An expression to schedule triggering of the rule every five minutes

Call the putRule method to create the rule. The callback returns the ARN of the new or updated rule.

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 CloudWatchEvents service object var cwevents = new AWS.CloudWatchEvents({apiVersion: '2015-10-07'}); var params = { Name: 'DEMO_EVENT', RoleArn: 'IAM_ROLE_ARN', ScheduleExpression: 'rate(5 minutes)', State: 'ENABLED' }; cwevents.putRule(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.RuleArn); } });

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

Copy
node cwe_putrule.js

This sample code can be found here on GitHub.

Adding a Lambda Function Target

Create a Node.js module with the file name cwe_puttargets.js. Be sure to configure the SDK as previously shown. To access CloudWatch Events, create an AWS.CloudWatchEvents service object. Create a JSON object containing the parameters needed to specify the rule to which you want to attach the target, including the ARN of the Lambda function you created. Call the putTargets method of the AWS.CloudWatchEvents 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 CloudWatchEvents service object var cwevents = new AWS.CloudWatchEvents({apiVersion: '2015-10-07'}); var params = { Rule: 'DEMO_EVENT', Targets: [ { Arn: 'LAMBDA_FUNCTION_ARN', Id: 'myCloudWatchEventsTarget', } ] }; cwevents.putTargets(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 cwe_puttargets.js

This sample code can be found here on GitHub.

Sending Events

Create a Node.js module with the file name cwe_putevents.js. Be sure to configure the SDK as previously shown. To access CloudWatch Events, create an AWS.CloudWatchEvents service object. Create a JSON object containing the parameters needed to send events. For each event, include the source of the event, the ARNs of any resources affected by the event, and details for the event. Call the putEvents method of the AWS.CloudWatchEvents 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 CloudWatchEvents service object var cwevents = new AWS.CloudWatchEvents({apiVersion: '2015-10-07'}); var params = { Entries: [ { Detail: '{ \"key1\": \"value1\", \"key2\": \"value2\" }', DetailType: 'appRequestSubmitted', Resources: [ 'RESOURCE_ARN', ], Source: 'com.company.myapp' } ] }; cwevents.putEvents(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Entries); } });

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

Copy
node cwe_putevents.js

This sample code can be found here on GitHub.