View a markdown version of this page

CloudWatch examples using SDK for JavaScript (v3) - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

CloudWatch examples using SDK for JavaScript (v3)

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for JavaScript (v3) with CloudWatch.

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.

Each example includes a link to the complete source code, 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 use DeleteAlarmMuteRule.

SDK for JavaScript (v3)
Note

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

import { DeleteAlarmMuteRuleCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Delete an alarm mute rule. The alarms it targeted resume firing their actions. const run = async () => { const command = new DeleteAlarmMuteRuleCommand({ AlarmMuteRuleName: process.env.CLOUDWATCH_MUTE_RULE_NAME, // Set CLOUDWATCH_MUTE_RULE_NAME to the name of an existing mute rule. }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

The following code example shows how to use DeleteAlarms.

SDK for JavaScript (v3)
Note

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

Import the SDK and client modules and call the API.

import { DeleteAlarmsCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; const run = async () => { const command = new DeleteAlarmsCommand({ AlarmNames: [process.env.CLOUDWATCH_ALARM_NAME], // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm. }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create the client in a separate module and export it.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});

The following code example shows how to use DescribeAlarmContributors.

SDK for JavaScript (v3)
Note

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

import { DescribeAlarmContributorsCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Get the contributors for a PromQL alarm. Each contributor is one series that the // alarm's query matched, identified by its label set. This is how you find out which // hosts, services, or pods are breaching, rather than only that something is. const run = async () => { const contributors = []; let nextToken; try { do { const command = new DescribeAlarmContributorsCommand({ AlarmName: process.env.CLOUDWATCH_ALARM_NAME, // Set CLOUDWATCH_ALARM_NAME to the name of an existing PromQL alarm. NextToken: nextToken, }); const response = await client.send(command); contributors.push(...(response.AlarmContributors ?? [])); nextToken = response.NextToken; } while (nextToken); for (const contributor of contributors) { const labels = Object.entries(contributor.ContributorAttributes) .map(([key, value]) => `${key}=${value}`) .join(", "); console.log(`${contributor.ContributorId}: ${labels}`); console.log(` reason: ${contributor.StateReason}`); } return contributors; } catch (err) { console.error(err); } }; export default run();

The following code example shows how to use DescribeAlarmsForMetric.

SDK for JavaScript (v3)
Note

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

Import the SDK and client modules and call the API.

import { DescribeAlarmsCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; const run = async () => { const command = new DescribeAlarmsCommand({ AlarmNames: [process.env.CLOUDWATCH_ALARM_NAME], // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm. }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create the client in a separate module and export it.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});

The following code example shows how to use DisableAlarmActions.

SDK for JavaScript (v3)
Note

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

Import the SDK and client modules and call the API.

import { DisableAlarmActionsCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; const run = async () => { const command = new DisableAlarmActionsCommand({ AlarmNames: process.env.CLOUDWATCH_ALARM_NAME, // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm. }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create the client in a separate module and export it.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});

The following code example shows how to use EnableAlarmActions.

SDK for JavaScript (v3)
Note

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

Import the SDK and client modules and call the API.

import { EnableAlarmActionsCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; const run = async () => { const command = new EnableAlarmActionsCommand({ AlarmNames: [process.env.CLOUDWATCH_ALARM_NAME], // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm. }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create the client in a separate module and export it.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});

The following code example shows how to use GetAlarmMuteRule.

SDK for JavaScript (v3)
Note

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

import { GetAlarmMuteRuleCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Get the full configuration of an alarm mute rule, including its schedule, the alarms // it targets, and whether it is currently SCHEDULED, ACTIVE, or EXPIRED. const run = async () => { const command = new GetAlarmMuteRuleCommand({ AlarmMuteRuleName: process.env.CLOUDWATCH_MUTE_RULE_NAME, // Set CLOUDWATCH_MUTE_RULE_NAME to the name of an existing mute rule. }); try { const response = await client.send(command); console.log(`Mute rule ${response.Name} is ${response.Status}.`); return response; } catch (err) { console.error(err); } }; export default run();
  • For API details, see GetAlarmMuteRule in AWS SDK for JavaScript API Reference.

The following code example shows how to use GetOTelEnrichment.

SDK for JavaScript (v3)
Note

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

import { GetOTelEnrichmentCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Get the current OTel enrichment status for the account. Status is either // "Running" or "Stopped". const run = async () => { const command = new GetOTelEnrichmentCommand({}); try { const response = await client.send(command); console.log(`OTel enrichment status is ${response.Status}.`); return response; } catch (err) { console.error(err); } }; export default run();

The following code example shows how to use ListAlarmMuteRules.

SDK for JavaScript (v3)
Note

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

import { ListAlarmMuteRulesCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // List the alarm mute rules in the account. Filter by the alarm they target, by // status, or both. const run = async () => { const summaries = []; let nextToken; try { do { const command = new ListAlarmMuteRulesCommand({ AlarmName: process.env.CLOUDWATCH_ALARM_NAME, // Set CLOUDWATCH_ALARM_NAME to filter to rules targeting one alarm. Statuses: ["SCHEDULED", "ACTIVE"], // Valid values: SCHEDULED, ACTIVE, EXPIRED. NextToken: nextToken, }); const response = await client.send(command); summaries.push(...(response.AlarmMuteRuleSummaries ?? [])); nextToken = response.NextToken; } while (nextToken); for (const summary of summaries) { console.log(`${summary.AlarmMuteRuleArn} (${summary.Status})`); } return summaries; } catch (err) { console.error(err); } }; export default run();

The following code example shows how to use ListMetrics.

SDK for JavaScript (v3)
Note

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

Import the SDK and client modules and call the API.

import { CloudWatchServiceException, ListMetricsCommand, } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; export const main = async () => { // Use the AWS console to see available namespaces and metric names. Custom metrics can also be created. // https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/viewing_metrics_with_cloudwatch.html const command = new ListMetricsCommand({ Dimensions: [ { Name: "LogGroupName", }, ], MetricName: "IncomingLogEvents", Namespace: "AWS/Logs", }); try { const response = await client.send(command); console.log(`Metrics count: ${response.Metrics?.length}`); return response; } catch (caught) { if (caught instanceof CloudWatchServiceException) { console.error(`Error from CloudWatch. ${caught.name}: ${caught.message}`); } else { throw caught; } } };

Create the client in a separate module and export it.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});

The following code example shows how to use PutAlarmMuteRule.

SDK for JavaScript (v3)
Note

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

import { PutAlarmMuteRuleCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Create or update an alarm mute rule. While a mute rule is active the targeted alarms // keep evaluating and keep transitioning between states, but their configured actions // do not fire. This is the supported way to suppress notifications during a known // maintenance window, instead of disabling alarm actions and hoping someone remembers // to turn them back on. const run = async () => { const command = new PutAlarmMuteRuleCommand({ Name: process.env.CLOUDWATCH_MUTE_RULE_NAME, // Set CLOUDWATCH_MUTE_RULE_NAME to the name of the mute rule. Description: "Suppress checkout CPU pages during Sunday patching.", Rule: { Schedule: { // For a recurring window, use a five-field cron expression, // cron(Minutes Hours Day-of-month Month Day-of-week). Note that this is five // fields, not the six that Amazon EventBridge uses. For a one-time window, use // an at expression such as "at(2026-09-05T02:00)". Expression: "cron(0 2 * * SUN)", // How long the mute window lasts once it activates, in ISO 8601 duration // format, from PT1M (one minute) to P15D (15 days). Duration: "PT2H", Timezone: "America/Los_Angeles", }, }, // Target up to 100 alarms by name. Omit MuteTargets to mute every alarm in the // account. MuteTargets: { AlarmNames: [process.env.CLOUDWATCH_ALARM_NAME], // Set CLOUDWATCH_ALARM_NAME to the name of an existing alarm. }, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
  • For API details, see PutAlarmMuteRule in AWS SDK for JavaScript API Reference.

The following code example shows how to use PutMetricAlarm.

SDK for JavaScript (v3)
Note

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

Create an alarm that evaluates a PromQL query against OpenTelemetry metrics.

import { PutMetricAlarmCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Create an alarm that evaluates a PromQL query over OpenTelemetry metrics. // // A PromQL alarm differs from a classic metric alarm in a few ways. The query can match // many series at once, and each matching series is tracked separately as a contributor // (see describe-alarm-contributors.js). Instead of counting breaching periods, you // specify durations: a contributor moves to ALARM after it breaches continuously for // PendingPeriod seconds, and back to OK after it stops breaching for RecoveryPeriod // seconds. A PromQL alarm starts in OK rather than INSUFFICIENT_DATA. // // EvaluationCriteria is a union and is mutually exclusive with the classic MetricName // and Metrics parameters. When you use it you must also set EvaluationInterval, and you // must not set Period, Statistic, Threshold, ComparisonOperator, EvaluationPeriods, // DatapointsToAlarm, or TreatMissingData. const run = async () => { const command = new PutMetricAlarmCommand({ AlarmName: process.env.CLOUDWATCH_ALARM_NAME, // Set CLOUDWATCH_ALARM_NAME to the name of the alarm to create. AlarmDescription: "Average CPU over 80% per host for the checkout service.", EvaluationCriteria: { PromQLCriteria: { // The comparison belongs in the query itself. There is no separate Threshold. Query: 'avg by (host_name) (cpu_utilization_percent{service_name="checkout"}) > 80', PendingPeriod: 300, RecoveryPeriod: 120, }, }, // How often to run the query, in seconds. Valid values are 10, 20, 30, and any // multiple of 60, up to 3600. EvaluationInterval: 30, ActionsEnabled: false, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create an alarm that evaluates a single CloudWatch metric.

import { PutMetricAlarmCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; const run = async () => { // This alarm triggers when CPUUtilization exceeds 70% for one minute. const command = new PutMetricAlarmCommand({ AlarmName: process.env.CLOUDWATCH_ALARM_NAME, // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm. ComparisonOperator: "GreaterThanThreshold", EvaluationPeriods: 1, MetricName: "CPUUtilization", Namespace: "AWS/EC2", Period: 60, Statistic: "Average", Threshold: 70.0, ActionsEnabled: false, AlarmDescription: "Alarm when server CPU exceeds 70%", Dimensions: [ { Name: "InstanceId", Value: process.env.EC2_INSTANCE_ID, // Set the value of EC_INSTANCE_ID to the Id of an existing Amazon EC2 instance. }, ], Unit: "Percent", }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create the client in a separate module and export it.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});

The following code example shows how to use PutMetricData.

SDK for JavaScript (v3)
Note

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

Import the SDK and client modules and call the API.

import { PutMetricDataCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; const run = async () => { // See https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html#API_PutMetricData_RequestParameters // and https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html // for more information about the parameters in this command. const command = new PutMetricDataCommand({ MetricData: [ { MetricName: "PAGES_VISITED", Dimensions: [ { Name: "UNIQUE_PAGES", Value: "URLS", }, ], Unit: "None", Value: 1.0, }, ], Namespace: "SITE/TRAFFIC", }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create the client in a separate module and export it.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});

The following code example shows how to use StartOTelEnrichment.

SDK for JavaScript (v3)
Note

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

import { StartOTelEnrichmentCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Turn on OTel enrichment for the account. Once enrichment is running, CloudWatch // vended metrics that carry a resource identifier dimension - for example the EC2 // CPUUtilization metric with its InstanceId dimension - are decorated with resource // ARN and resource tag labels, and become queryable with PromQL. // // Resource tags on telemetry must already be enabled for the account before you call // this operation. const run = async () => { const command = new StartOTelEnrichmentCommand({}); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

The following code example shows how to use StopOTelEnrichment.

SDK for JavaScript (v3)
Note

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

import { StopOTelEnrichmentCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Turn off OTel enrichment for the account. Existing PromQL alarms are not deleted, // but vended metrics stop being enriched with resource ARN and tag labels, so queries // that select on those labels stop matching. const run = async () => { const command = new StopOTelEnrichmentCommand({}); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();