CloudWatch Logs examples using SDK for JavaScript V3 - 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 V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (v3).

CloudWatch Logs examples using SDK for JavaScript V3

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for JavaScript V3 with CloudWatch Logs.

Actions are code excerpts that show you how to call individual CloudWatch Logs functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple CloudWatch Logs functions.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to create an Amazon CloudWatch Logs subscription filter.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { CloudWatchLogsClient } from "@aws-sdk/client-cloudwatch-logs"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch Logs service client object. export const cwlClient = new CloudWatchLogsClient({ region: REGION });

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import { PutSubscriptionFilterCommand, } from "@aws-sdk/client-cloudwatch-logs"; import { cwlClient } from "./libs/cloudWatchLogsClient.js"; // Set the parameters export const params = { destinationArn: "LAMBDA_FUNCTION_ARN", //LAMBDA_FUNCTION_ARN filterName: "FILTER_NAME", //FILTER_NAME filterPattern: "ERROR", logGroupName: "LOG_GROUP", //LOG_GROUP }; export const run = async () => { try { const data = await cwlClient.send(new PutSubscriptionFilterCommand(params)); console.log("Success", data.subscriptionFilters); return data; //For unit tests. } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
SDK for JavaScript V2
Tip

To learn how to set up and run this example, see GitHub.

// 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); } });

The following code example shows how to delete an Amazon CloudWatch Logs subscription filter.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { CloudWatchLogsClient } from "@aws-sdk/client-cloudwatch-logs"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch Logs service client object. export const cwlClient = new CloudWatchLogsClient({ region: REGION });

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import { DeleteSubscriptionFilterCommand } from "@aws-sdk/client-cloudwatch-logs"; import { cwlClient } from "./libs/cloudWatchLogsClient.js"; // Set the parameters export const params = { filterName: "FILTER", //FILTER logGroupName: "LOG_GROUP", //LOG_GROUP }; export const run = async () => { try { const data = await cwlClient.send( new DeleteSubscriptionFilterCommand(params) ); console.log( "Success, subscription filter deleted", data ); return data; //For unit tests. } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
SDK for JavaScript V2
Tip

To learn how to set up and run this example, see GitHub.

// 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); } });

The following code example shows how to describe Amazon CloudWatch Logs existing subscription filters.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { CloudWatchLogsClient } from "@aws-sdk/client-cloudwatch-logs"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch Logs service client object. export const cwlClient = new CloudWatchLogsClient({ region: REGION });

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import { DescribeSubscriptionFiltersCommand } from "@aws-sdk/client-cloudwatch-logs"; import { cwlClient } from "./libs/cloudWatchLogsClient.js"; // Set the parameters export const params = { logGroupName: "GROUP_NAME", //GROUP_NAME limit: 5 }; export const run = async () => { try { const data = await cwlClient.send( new DescribeSubscriptionFiltersCommand(params) ); console.log("Success", data.subscriptionFilters); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
SDK for JavaScript V2
Tip

To learn how to set up and run this example, see GitHub.

// 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); } });