Tutorial: Create a sample Amazon EventBridge application
You can use EventBridge to route events to specific Lambda functions using rules.
In this tutorial, you’ll use the Amazon CLI, Node.js, and the code in the GitHub
repo
An Amazon Lambda function that produces events for bank ATM transactions.
Three Lambda functions to use as targets of an EventBridge rule.
and the rule that routes the created events to the correct downstream function based on an event pattern.
This example uses Amazon SAM templates to define the EventBridge rules. To learn more about using Amazon SAM templates with EventBridge see Using Amazon Serverless Application Model templates to deploy Amazon EventBridge resources.
In the repo, the atmProducer subdirectory contains handler.js
, which represents the ATM service producing events.
This code is a Lambda handler written in Node.js, and publishes events to EventBridge via the Amazon SDK
const result = await eventbridge.putEvents(params).promise()
This directory also contains events.js
, listing several test transactions in an Entries array. A single event is defined in JavaScript as follows:
{ // Event envelope fields Source: 'custom.myATMapp', EventBusName: 'default', DetailType: 'transaction', Time: new Date(), // Main event body Detail: JSON.stringify({ action: 'withdrawal', location: 'MA-BOS-01', amount: 300, result: 'approved', transactionId: '123456', cardPresent: true, partnerBank: 'Example Bank', remainingFunds: 722.34 }) }
The Detail section of the event specifies transaction attributes. These include the location of the ATM, the amount, the partner bank, and the result of the transaction.
The handler.js
file in the atmConsumer subdirectory contains three functions:
exports.case1Handler = async (event) => { console.log('--- Approved transactions ---') console.log(JSON.stringify(event, null, 2)) } exports.case2Handler = async (event) => { console.log('--- NY location transactions ---') console.log(JSON.stringify(event, null, 2)) } exports.case3Handler = async (event) => { console.log('--- Unapproved transactions ---') console.log(JSON.stringify(event, null, 2)) }
Each function receives transaction events, which are logged via the console.log
statements
to Amazon CloudWatch Logs. The consumer functions operate independently of the producer and are unaware of the source
of the events.
The routing logic is contained in the EventBridge rules that are deployed by the application’s Amazon SAM template. The rules evaluate the incoming stream of events, and route matching events to the target Lambda functions.
The rules use event patterns that are JSON objects with the same structure as the events they match. Here's the event pattern for the one of the rules.
{ "detail-type": ["transaction"], "source": ["custom.myATMapp"], "detail": { "location": [{ "prefix": "NY-" }] } }
Steps:
Prerequisites
To complete this tutorial, you'll need the following resources:
An Amazon account. Create an Amazon account
if you don't already have one. Amazon CLI installed. To install the Amazon CLI, see the Installing, updating, and uninstalling the Amazon CLI version 2.
Node.js 12.x installed. To install Node.js, see Downloads
.
Step 1: Create application
To set up the example application, you'll use the Amazon CLI and Git to create the Amazon resources you'll need.
To create the application
-
Install Git
and install the Amazon Serverless Application Model CLI on your local machine. -
Create a new directory, and then navigate to that directory in a terminal.
-
At the command line, enter
git clone https://github.com/aws-samples/amazon-eventbridge-producer-consumer-example
. -
At the command line run the following command:
cd ./amazon-eventbridge-producer-consumer-example sam deploy --guided
-
In the terminal, do the following:
-
For
Stack Name
, enter a name for the stack. For example, name the stackTest
. -
For
AWS Region
, enter the Region. For example,us-west-2
. -
For
Confirm changes before deploy
, enterY
. -
For
Allow SAM CLI IAM role creation
, enterY
-
For
Save arguments to configuration file
, enterY
-
For
SAM configuration file
, entersamconfig.toml
. -
For
SAM configuration environment
, enterdefault
.
-
Step 2: Run application
Now that you've set up the resources, you'll use the console to test the functions.
To run the application
Open the Lambda console
in the same Region where you deployed the Amazon SAM application. There are four Lambda functions with the prefix atm-demo. Select the atmProducerFn function, then choose Actions, Test.
Enter
Test
for the Name.Choose Test.
Step 3: Check the logs and verify the application works
Now that you've run the application, you'll use the console to check the CloudWatch Logs.
To check the logs
Open the CloudWatch console
in the same Region where you ran the Amazon SAM application. Choose Logs, and then choose Log groups.
Select the log group containing atmConsumerCase1. You see two streams representing the two transactions approved by the ATM. Choose a log stream to view the output.
Navigate back to the list of log groups, and then select the log group containing atmConsumerCase2. You'll see two streams representing the two transactions matching the New York location filter.
Navigate back to the list of log groups, and select the log group containing atmConsumerCase3. Open the stream to see the denied transactions.
Step 4: Clean up your resources
You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting Amazon resources that you are no longer using, you prevent unnecessary charges to your Amazon account.
To delete the EventBridge rule(s)
-
Open the Rules page
of the EventBridge console. -
Select the rule(s) that you created.
-
Choose Delete.
-
Choose Delete.
To delete the Lambda function(s)
-
Open the Functions page
of the Lambda console. -
Select the function(s) that you created.
-
Choose Actions, Delete.
-
Choose Delete.
To delete the CloudWatch Logs log group(s)
-
Open the Cloudwatch console
. -
Choose Logs, Log groups.
-
Select the log group(s) that were created in this tutorial.
-
Choose Actions, Delete log group(s).
-
Choose Delete.