CloudWatch Events examples using SDK for JavaScript (v3) - AWS SDK for JavaScript

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (V3).

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 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({});