Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

CloudWatch Events 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.

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

CloudWatch Events 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 Events.

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

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 { PutEventsCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutEventsCommand({ // The list of events to send to Amazon CloudWatch Events. Entries: [ { // The name of the application or service that is sending the event. Source: "my.app", // The name of the event that is being sent. DetailType: "My Custom Event", // The data that is sent with the event. Detail: JSON.stringify({ timeOfEvent: new Date().toISOString() }), }, ], }); 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 { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});

The following code example shows how to use PutEvents.

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 { PutEventsCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutEventsCommand({ // The list of events to send to Amazon CloudWatch Events. Entries: [ { // The name of the application or service that is sending the event. Source: "my.app", // The name of the event that is being sent. DetailType: "My Custom Event", // The data that is sent with the event. Detail: JSON.stringify({ timeOfEvent: new Date().toISOString() }), }, ], }); 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 { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});

The following code example shows how to use PutRule.

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 { PutRuleCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { // Request parameters for PutRule. // https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutRule.html#API_PutRule_RequestParameters const command = new PutRuleCommand({ Name: process.env.CLOUDWATCH_EVENTS_RULE, // The event pattern for the rule. // Example: {"source": ["my.app"]} EventPattern: process.env.CLOUDWATCH_EVENTS_RULE_PATTERN, // The state of the rule. Valid values: ENABLED, DISABLED State: "ENABLED", }); 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 { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});

The following code example shows how to use PutRule.

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 { PutRuleCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { // Request parameters for PutRule. // https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutRule.html#API_PutRule_RequestParameters const command = new PutRuleCommand({ Name: process.env.CLOUDWATCH_EVENTS_RULE, // The event pattern for the rule. // Example: {"source": ["my.app"]} EventPattern: process.env.CLOUDWATCH_EVENTS_RULE_PATTERN, // The state of the rule. Valid values: ENABLED, DISABLED State: "ENABLED", }); 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 { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});

The following code example shows how to use PutTargets.

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 { PutTargetsCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutTargetsCommand({ // The name of the Amazon CloudWatch Events rule. Rule: process.env.CLOUDWATCH_EVENTS_RULE, // The targets to add to the rule. Targets: [ { Arn: process.env.CLOUDWATCH_EVENTS_TARGET_ARN, // The ID of the target. Choose a unique ID for each target. Id: process.env.CLOUDWATCH_EVENTS_TARGET_ID, }, ], }); 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 { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});

The following code example shows how to use PutTargets.

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 { PutTargetsCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutTargetsCommand({ // The name of the Amazon CloudWatch Events rule. Rule: process.env.CLOUDWATCH_EVENTS_RULE, // The targets to add to the rule. Targets: [ { Arn: process.env.CLOUDWATCH_EVENTS_TARGET_ARN, // The ID of the target. Choose a unique ID for each target. Id: process.env.CLOUDWATCH_EVENTS_TARGET_ID, }, ], }); 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 { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});
PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.