Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

EventBridge to Lambda

Created with SnapEventBridgeAWS Lambda

Create an EventBridge rule that invokes a Lambda function

This template deploys a Lambda function that is triggered by an EventBridge rule. In this example, the rule filters for specific attributes in the event before invoking the function.
The Events section of the AWS::Serverless::Function type also sets up the required permissions for EventBridge to invoke this specific function.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Serverless patterns - EventBridge to Lambda (uksb-1tthgi812) (tag:eventbridge-lambda)
  
Resources:
  # Define the consumer Lambda function and EventBridge rule
  ConsumerFunction:
    Type: AWS::Serverless::Function
    Description: Invoked by EventBridge rule
    Properties:
      CodeUri: src/
      Handler: app.handler
      Runtime: nodejs20.x
      Timeout: 3
      Events:
        Trigger:
          Type: EventBridgeRule 
          Properties:
            Pattern:
              source:
                - custom.myApp
              detail-type:
                - transaction                
              detail:
                location:
                  - "prefix": "EUR-"

Outputs:
  ConsumerFunction:
    Description: ConsumerFunction function name
    Value: !Ref ConsumerFunction

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/eventbridge-lambda

Deploy

sam deploy --guided


Testing

Use the AWS CLI to send a test event to EventBridge and observe the event delivered to the Lambda function:
1. Send an event to EventBridge: aws events put-events --entries file://event.json.
2. Retrieve the logs from the Lambda function: sam logs -n ENTER_YOUR_CONSUMER_FUNCTION_NAME.

Cleanup

1. Delete the stack: sam delete --stack-name STACK_NAME.
2. Confirm the stack has been deleted: aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"

James Beswick

Presented by James Beswick

I am a self-confessed serverless geek and enjoy helping developers build faster with serverless! I have been software developer and product manager before becoming a Developer Advocate. Ask me anything!

Follow on LinkedIn