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 (PDF).

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).

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 from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

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 a new CloudWatch Logs log group.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import { CreateLogGroupCommand } from "@aws-sdk/client-cloudwatch-logs"; import { client } from "../libs/client.js"; const run = async () => { const command = new CreateLogGroupCommand({ // The name of the log group. logGroupName: process.env.CLOUDWATCH_LOGS_LOG_GROUP, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
  • For API details, see CreateLogGroup in Amazon SDK for JavaScript API Reference.

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

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import { PutSubscriptionFilterCommand } from "@aws-sdk/client-cloudwatch-logs"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutSubscriptionFilterCommand({ // An ARN of a same-account Kinesis stream, Kinesis Firehose // delivery stream, or Lambda function. // https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html destinationArn: process.env.CLOUDWATCH_LOGS_DESTINATION_ARN, // A name for the filter. filterName: process.env.CLOUDWATCH_LOGS_FILTER_NAME, // A filter pattern for subscribing to a filtered stream of log events. // https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html filterPattern: process.env.CLOUDWATCH_LOGS_FILTER_PATTERN, // The name of the log group. Messages in this group matching the filter pattern // will be sent to the destination ARN. logGroupName: process.env.CLOUDWATCH_LOGS_LOG_GROUP, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
SDK for JavaScript (v2)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

// 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 existing CloudWatch Logs log group.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import { DeleteLogGroupCommand } from "@aws-sdk/client-cloudwatch-logs"; import { client } from "../libs/client.js"; const run = async () => { const command = new DeleteLogGroupCommand({ // The name of the log group. logGroupName: process.env.CLOUDWATCH_LOGS_LOG_GROUP, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
  • For API details, see DeleteLogGroup in Amazon SDK for JavaScript API Reference.

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

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import { DeleteSubscriptionFilterCommand } from "@aws-sdk/client-cloudwatch-logs"; import { client } from "../libs/client.js"; const run = async () => { const command = new DeleteSubscriptionFilterCommand({ // The name of the filter. filterName: process.env.CLOUDWATCH_LOGS_FILTER_NAME, // The name of the log group. logGroupName: process.env.CLOUDWATCH_LOGS_LOG_GROUP, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
SDK for JavaScript (v2)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

// 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)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import { DescribeSubscriptionFiltersCommand } from "@aws-sdk/client-cloudwatch-logs"; import { client } from "../libs/client.js"; const run = async () => { // This will return a list of all subscription filters in your account // matching the log group name. const command = new DescribeSubscriptionFiltersCommand({ logGroupName: process.env.CLOUDWATCH_LOGS_LOG_GROUP, limit: 1, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
SDK for JavaScript (v2)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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

The following code example shows how to describe CloudWatch Logs log groups.

SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import { paginateDescribeLogGroups, CloudWatchLogsClient, } from "@aws-sdk/client-cloudwatch-logs"; const client = new CloudWatchLogsClient({}); export const main = async () => { const paginatedLogGroups = paginateDescribeLogGroups({ client }, {}); const logGroups = []; for await (const page of paginatedLogGroups) { if (page.logGroups && page.logGroups.every((lg) => !!lg)) { logGroups.push(...page.logGroups); } } console.log(logGroups); return logGroups; };