Create an Amazon EventBridge sample application - Amazon EventBridge
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).

Create an Amazon EventBridge sample 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 to create the following:

  • 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 EventBridge and Amazon Serverless Application Model templates.

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 using this line of JavaScript code.

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-" }] } }


To complete this tutorial, you'll need the following resources:

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
  1. Sign in to Amazon.

  2. Install Git and install the Amazon Serverless Application Model CLI on your local machine.

  3. Create a new directory, and then navigate to that directory in a terminal.

  4. At the command line, enter git clone

  5. At the command line run the following command:

    cd ./amazon-eventbridge-producer-consumer-example sam deploy --guided
  6. In the terminal, do the following:

    1. For Stack Name, enter a name for the stack. For example, name the stack Test.

    2. For AWS Region, enter the Region. For example, us-west-2.

    3. For Confirm changes before deploy, enter Y.

    4. For Allow SAM CLI IAM role creation, enter Y

    5. For Save arguments to configuration file, enter Y

    6. For SAM configuration file, enter samconfig.toml.

    7. For SAM configuration environment, enter default.

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
  1. Open the Lambda console in the same Region where you deployed the Amazon SAM application.

  2. There are four Lambda functions with the prefix atm-demo. Select the atmProducerFn function, then choose Actions, Test.

  3. Enter Test for the Name.

  4. 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
  1. Open the CloudWatch console in the same Region where you ran the Amazon SAM application.

  2. Choose Logs, and then choose Log groups.

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

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

  5. 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)
  1. Open the Rules page of the EventBridge console.

  2. Select the rule(s) that you created.

  3. Choose Delete.

  4. Choose Delete.

To delete the Lambda function(s)
  1. Open the Functions page of the Lambda console.

  2. Select the function(s) that you created.

  3. Choose Actions, Delete.

  4. Choose Delete.

To delete the CloudWatch Logs log group(s)
  1. Open the Cloudwatch console.

  2. Choose Logs, Log groups.

  3. Select the log group(s) that were created in this tutorial.

  4. Choose Actions, Delete log group(s).

  5. Choose Delete.