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

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

Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.

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.

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 { 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 AWS 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 AWS 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 AWS 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 AWS 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 AWS 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 AWS 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 AWS 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 AWS 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 AWS 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 AWS 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 AWS SDK for JavaScript API Reference.

Scenarios

The following code example shows how to create an AWS Lambda function invoked by an Amazon EventBridge scheduled event.

SDK for JavaScript (v3)

Shows how to create an Amazon EventBridge scheduled event that invokes an AWS Lambda function. Configure EventBridge to use a cron expression to schedule when the Lambda function is invoked. In this example, you create a Lambda function by using the Lambda JavaScript runtime API. This example invokes different AWS services to perform a specific use case. This example demonstrates how to create an app that sends a mobile text message to your employees that congratulates them at the one year anniversary date.

For complete source code and instructions on how to set up and run, see the full example on GitHub.

This example is also available in the AWS SDK for JavaScript v3 developer guide.

Services used in this example
  • DynamoDB

  • EventBridge

  • Lambda

  • Amazon SNS