Using Subscription Filters in Amazon CloudWatch Logs - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

The Amazon SDK for JavaScript version 3 (v3) is a rewrite of v2 with some great new features, including modular architecture. For more information, see the Amazon SDK for JavaScript v3 Developer Guide.

Using Subscription Filters in Amazon CloudWatch Logs

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to create and delete filters for log events in CloudWatch Logs.

The Scenario

Subscriptions provide access to a real-time feed of log events from CloudWatch Logs and deliver that feed to other services, such as an Amazon Kinesis stream or Amazon Lambda, for custom processing, analysis, or loading to other systems. A subscription filter defines the pattern to use for filtering which log events are delivered to your Amazon resource.

In this example, a series of Node.js modules are used to list, create, and delete a subscription filter in CloudWatch Logs. The destination for the log events is a Lambda function. The Node.js modules use the SDK for JavaScript to manage subscription filters using these methods of the CloudWatchLogs client class:

For more information about CloudWatch Logs subscriptions, see Real-time Processing of Log Data with Subscriptions in the Amazon CloudWatch Logs User Guide.

Prerequisite Tasks

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

  • Install Node.js. For more information about installing Node.js, see the Node.js website.

  • Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Loading Credentials in Node.js from the Shared Credentials File.

  • Create a Lambda function as the destination for log events. You will need to use the ARN of this function. For more information about setting up a Lambda function, see Subscription Filters with Amazon Lambda in the Amazon CloudWatch Logs User Guide.

  • Create an IAM role whose policy grants permission to invoke the Lambda function you created and grants full access to CloudWatch Logs or apply the following policy to the execution role you create for the Lambda function. For more information about creating an IAM role, see Creating a Role to Delegate Permissions to an Amazon Service in the IAM User Guide.

Use the following role policy when creating the IAM role.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "lambda:InvokeFunction" ], "Resource": [ "*" ] } ] }

Describing Existing Subscription Filters

Create a Node.js module with the file name cwl_describesubscriptionfilters.js. Be sure to configure the SDK as previously shown. To access CloudWatch Logs, create an AWS.CloudWatchLogs service object. Create a JSON object containing the parameters needed to describe your existing filters, including the name of the log group and the maximum number of filters you want described. Call the describeSubscriptionFilters method.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the CloudWatchLogs service object var cwl = new AWS.CloudWatchLogs({apiVersion: '2014-03-28'}); var params = { logGroupName: 'GROUP_NAME', limit: 5 }; cwl.describeSubscriptionFilters(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.subscriptionFilters); } });

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

node cwl_describesubscriptionfilters.js

This sample code can be found here on GitHub.

Creating a Subscription Filter

Create a Node.js module with the file name cwl_putsubscriptionfilter.js. Be sure to configure the SDK as previously shown. To access CloudWatch Logs, create an AWS.CloudWatchLogs service object. Create a JSON object containing the parameters needed to create a filter, including the ARN of the destination Lambda function, the name of the filter, the string pattern for filtering, and the name of the log group. Call the putSubscriptionFilters method.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the CloudWatchLogs service object var cwl = new AWS.CloudWatchLogs({apiVersion: '2014-03-28'}); var params = { destinationArn: 'LAMBDA_FUNCTION_ARN', filterName: 'FILTER_NAME', filterPattern: 'ERROR', logGroupName: 'LOG_GROUP', }; cwl.putSubscriptionFilter(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.

node cwl_putsubscriptionfilter.js

This sample code can be found here on GitHub.

Deleting a Subscription Filter

Create a Node.js module with the file name cwl_deletesubscriptionfilters.js. Be sure to configure the SDK as previously shown. To access CloudWatch Logs, create an AWS.CloudWatchLogs service object. Create a JSON object containing the parameters needed to delete a filter, including the names of the filter and the log group. Call the deleteSubscriptionFilters method.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the CloudWatchLogs service object var cwl = new AWS.CloudWatchLogs({apiVersion: '2014-03-28'}); var params = { filterName: 'FILTER', logGroupName: 'LOG_GROUP' }; cwl.deleteSubscriptionFilter(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.

node cwl_deletesubscriptionfilter.js

This sample code can be found here on GitHub.