Amazon EventBridge — Developing with AWS SDK to interact Serverless APIs

In this article, we are going to develop Amazon SQS interactions with using AWS SDK.

Amazon EventBridge — Developing with AWS SDK

We are going to do Amazon EventBridge SDK Examples with using AWS SDK JavaScript v3 and use ES6 standards. By the end of the article, we will invoke EventBridge API to perform topic related tasks using the latest AWS SDK.

I have just published a new course — AWS Lambda & Serverless — Developer Guide with Hands-on Labs.

Amazon EventBridge— Developing with AWS SDK

Amazon EventBridge is a Serverless event bus that makes it easier to build event-driven applications at scale using events generated from your applications, integrated SaaS applications and AWS services.

Amazon EventBridge helps you to respond to state changes in your Amazon Web Services resources. When your resources change state, they automatically send events to an event stream. We can create rules that match selected events in the stream and route them to targets to take action. We can also use rules to take action on a predetermined schedule.

Amazon EventBridge Interactions

The JavaScript API for Amazon EventBridge is exposed through the EventBridge client class. So when we are interacting with EventBridge, we will follow topics below :

  • Put events in Amazon EventBridge
  • Put event rule in Amazon EventBridge
  • Put event targets in Amazon EventBridge using AWS SDK
Amazon EventBridge — Developing with AWS SDK

How we can perform these use cases with AWS SDK ?

Put Events on Amazon EventBridge using AWS SDK

We are going to Developing Put Events on Amazon EventBridge using AWS SDK. We will use a series of Node.js modules are used to work with queues. The Node.js modules use the SDK for JavaScript to enable queues to call the following methods of the EventBridge client class:

  • PutEventsCommand

Create NodeJS Project with EventBridge SDK Packages

We are going to Create NodeJS Project with EventBridge SDK Packages to interact with EventBridge APIs. Open VS Code, create “EventBridge-sdk” folder. This will be our NodeJS project for this section.

Locate folder and run npm command in order to create package.json:

npm init -y

See and edit default package.json is created. The first thing we should do
Add “type”: “module”:

package.json 
“keywords”: [],
“author”: “”,
“license”: “ISC”,
“type”: “module” — — — ADDED
}

this provide to use ES6 codes into nodejs projects. for example “import statements”.

Install required EventBridge SDK packages

Run command on package.json level

npm install @aws-sdk/client-eventbridge

See package installed in package.json file: “@aws-sdk/client-eventbridge”

{
“name”: “section8”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“keywords”: [],
“author”: “”,
“license”: “ISC”,
“type”: “module”,
“dependencies”: {
@aws-sdk/client-eventbridge”: “³.87.0”
}
}

As you can see that node_modules are generated when running npm install command.

Create EventBridgeClient NodeJS Module For Connecting EventBridge

This section we will interact with SQS. For all these interactions we need to connect SQS, for that purpose we should establish SQSClient object. Instead of creating new client for every SDK request, its best practice to create NodeJS module and use this module for every particular SDK commands.

Amazon EventBridge — Developing with AWS SDK

Create a libs directory, and create a Node.js module with the file name ebClient.js. Copy and paste the code below into it, which creates the EventBridge client object. Replace REGION with your AWS region.

import { EventBridgeClient } from "@aws-sdk/client-eventbridge";
// Set the AWS Region.
const REGION = "us-east-2";
// Create an Amazon EventBridge service client object.
export const ebClient = new EventBridgeClient({ region: REGION });

As you can see that we have created NodeJS module for EventBridge connections with creating a new instance of EventBridgeClient object. This client object will use every interaction with our SDK examples.

Now we are ready to develop our Topics:

  • PutEventsCommand

PutEventsCommand

Create a Node.js module with the file name putevent.js. Be sure to configure the SDK as previously shown, including downloading the required clients and packages. We can call the PutEventsCommand method.

// Import required AWS SDK clients and commands for Node.js.
import { PutEventsCommand } from “@aws-sdk/client-eventbridge”;
import { ebClient } from “./libs/eventBridgeClient.js”;
// Set the parameters.
export const params = {
Entries: [
{
Detail: ‘{ “key1”: “value1”, “key2”: “value2” }’,
DetailType: “appRequestSubmitted”,
Resources: [
“RESOURCE_ARN”, //RESOURCE_ARN
],
Source: “com.company.app”,
},
],
};
export const run = async () => {
try {
const data = await ebClient.send(new PutEventsCommand(params));
console.log(“Success, event sent; requestID:”, data);
return data; // For unit tests.
} catch (err) {
console.log(“Error”, err);
}
};
run();

Put Event Rule on Amazon EventBridge using AWS SDK

Create a Node.js module with the file name putevent.js. Be sure to configure the SDK as previously shown, including downloading the required clients and packages. We can call the PutRuleCommand method to put event rule into EventBridge.

import { PutRuleCommand } from “@aws-sdk/client-eventbridge”;
import { ebClient } from “./libs/eventBridgeClient.js”;
// Set the parameters.
export const params = {
Name: “DEMO_EVENT”,
RoleArn: “IAM_ROLE_ARN”, //IAM_ROLE_ARN
ScheduleExpression: “rate(5 minutes)”,
State: “ENABLED”,
};
export const run = async () => {
try {
const data = await ebClient.send(new PutRuleCommand(params));
console.log(“Success, scheduled rule created; Rule ARN:”, data);
return data; // For unit tests.
} catch (err) {
console.log(“Error”, err);
}
};

Put Event Target on Amazon EventBridge using AWS SDK

Create a Node.js module with the file name puteventtarget.js. Be sure to configure the SDK as previously shown, including downloading the required clients and packages. We can call the PutTargetsCommand method to put event rule into EventBridge.

import { PutTargetsCommand } from “@aws-sdk/client-eventbridge”;
import { ebClient } from “./libs/eventBridgeClient.js”;
// Set the parameters.
export const params = {
Rule: “DEMO_EVENT”,
Targets: [
{
Arn: “LAMBDA_FUNCTION_ARN”, //LAMBDA_FUNCTION_ARN
Id: “myCloudWatchEventsTarget”,
},
],
};
export const run = async () => {
try {
const data = await ebClient.send(new PutTargetsCommand(params));
console.log(“Success, target added; requestID: “, data);
return data; // For unit tests.
} catch (err) {
console.log(“Error”, err);
}
};

I am going to skip other commands but if you would like to continue, you can follow the below course.

Step by Step Design AWS Architectures w/ Course

I have just published a new course — AWS Lambda & Serverless — Developer Guide with Hands-on Labs.

In this course, we will learn almost all the AWS Serverless Services with all aspects. We are going to build serverless applications with using AWS Lambda, Amazon API Gateway, Amazon DynamoDB, Amazon Cognito, Amazon S3, Amazon SNS, Amazon SQS, Amazon EventBridge, AWS Step Functions, DynamoDB and Kinesis Streams. This course will be 100% hands-on, and you will be developing a real-world application with hands-on labs together and step by step.

Source Code

Get the Source Code from Serverless Microservices GitHub — Clone or fork this repository, if you like don’t forget the star. If you find or ask anything you can directly open issue on repository.

--

--

Mehmet Ozkaya
AWS Lambda & Serverless — Developer Guide with Hands-on Labs

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices https://github.com/mehmetozkaya