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

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

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 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 Amazon Code Examples Repository.

Import the SDK and client modules and call the API.

import { EventBridgeClient, PutEventsCommand, } from "@aws-sdk/client-eventbridge"; export const putEvents = async ( source = "eventbridge.integration.test", detailType = "greeting", resources = [], ) => { const client = new EventBridgeClient({}); const response = await client.send( new PutEventsCommand({ Entries: [ { Detail: JSON.stringify({ greeting: "Hello there." }), DetailType: detailType, Resources: resources, Source: source, }, ], }), ); console.log("PutEvents response:"); console.log(response); // PutEvents response: // { // '$metadata': { // httpStatusCode: 200, // requestId: '3d0df73d-dcea-4a23-ae0d-f5556a3ac109', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // Entries: [ { EventId: '51620841-5af4-6402-d9bc-b77734991eb5' } ], // FailedEntryCount: 0 // } return response; };
  • For API details, see PutEvents in Amazon SDK for JavaScript API Reference.

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 CloudWatchEvents service object var ebevents = new AWS.EventBridge({ apiVersion: "2015-10-07" }); var params = { Entries: [ { Detail: '{ "key1": "value1", "key2": "value2" }', DetailType: "appRequestSubmitted", Resources: ["RESOURCE_ARN"], Source: "com.company.app", }, ], }; ebevents.putEvents(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Entries); } });
  • For API details, see PutEvents in Amazon SDK for JavaScript API Reference.

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 Amazon Code Examples Repository.

Import the SDK and client modules and call the API.

import { EventBridgeClient, PutRuleCommand } from "@aws-sdk/client-eventbridge"; export const putRule = async ( ruleName = "some-rule", source = "some-source", ) => { const client = new EventBridgeClient({}); const response = await client.send( new PutRuleCommand({ Name: ruleName, EventPattern: JSON.stringify({ source: [source] }), State: "ENABLED", EventBusName: "default", }), ); console.log("PutRule response:"); console.log(response); // PutRule response: // { // '$metadata': { // httpStatusCode: 200, // requestId: 'd7292ced-1544-421b-842f-596326bc7072', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // RuleArn: 'arn:aws:events:us-east-1:xxxxxxxxxxxx:rule/EventBridgeTestRule-1696280037720' // } return response; };
  • For API details, see PutRule in Amazon SDK for JavaScript API Reference.

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 CloudWatchEvents service object var ebevents = new AWS.EventBridge({ apiVersion: "2015-10-07" }); var params = { Name: "DEMO_EVENT", RoleArn: "IAM_ROLE_ARN", ScheduleExpression: "rate(5 minutes)", State: "ENABLED", }; ebevents.putRule(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.RuleArn); } });
  • For API details, see PutRule in Amazon SDK for JavaScript API Reference.

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 Amazon Code Examples Repository.

Import the SDK and client modules and call the API.

import { EventBridgeClient, PutTargetsCommand, } from "@aws-sdk/client-eventbridge"; export const putTarget = async ( existingRuleName = "some-rule", targetArn = "arn:aws:lambda:us-east-1:000000000000:function:test-func", uniqueId = Date.now().toString(), ) => { const client = new EventBridgeClient({}); const response = await client.send( new PutTargetsCommand({ Rule: existingRuleName, Targets: [ { Arn: targetArn, Id: uniqueId, }, ], }), ); console.log("PutTargets response:"); console.log(response); // PutTargets response: // { // '$metadata': { // httpStatusCode: 200, // requestId: 'f5b23b9a-2c17-45c1-ad5c-f926c3692e3d', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // FailedEntries: [], // FailedEntryCount: 0 // } return response; };
  • For API details, see PutTargets in Amazon SDK for JavaScript API Reference.

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 CloudWatchEvents service object var ebevents = new AWS.EventBridge({ apiVersion: "2015-10-07" }); var params = { Rule: "DEMO_EVENT", Targets: [ { Arn: "LAMBDA_FUNCTION_ARN", Id: "myEventBridgeTarget", }, ], }; ebevents.putTargets(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
  • For API details, see PutTargets in Amazon SDK for JavaScript API Reference.