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

Using Amazon EventBridge and Amazon Serverless Application Model templates

You can build and test rules manually in the EventBridge console, which can help in the development process as you refine event patterns. However, once you are ready to deploy your application, it’s easier to use a framework like Amazon SAM to launch all your serverless resources consistently.

We'll use this example application to look into the ways you can use Amazon SAM templates to build EventBridge resources. The template.yaml file in this example is a Amazon SAM template that defines four Amazon Lambda functions and shows two different ways to integrate the Lambda functions with EventBridge.

For a walkthrough of this example application, see Create an Amazon EventBridge sample application.

There are two approaches to using EventBridge and Amazon SAM templates. For simple integrations where one Lambda function is invoked by one rule, the the Combined template approach is recommended. If you have complex routing logic, or you are connecting to resources outside of your Amazon SAM template, the Separated template approach is the better choice.

Combined template

The first approach uses the Events property to configure the EventBridge rule. The following example code defines an event that invokes your Lambda function.

Note

This example automatically creates the rule on the default event bus, which exists in every Amazon account. To associate the rule with a custom event bus, you can add the EventBusName to the template.

atmConsumerCase3Fn: Type: AWS::Serverless::Function Properties: CodeUri: atmConsumer/ Handler: handler.case3Handler Runtime: nodejs12.x Events: Trigger: Type: CloudWatchEvent Properties: Pattern: source: - custom.myATMapp detail-type: - transaction detail: result: - "anything-but": "approved"

This YAML code is equivalent to an event pattern in the EventBridge console. In YAML, you only need to define the event pattern, and Amazon SAM automatically creates an IAM role with the required permissions.

Separated template

In the second approach to defining an EventBridge configuration in Amazon SAM, the resources are separated more clearly in the template.

  1. First, you define the Lambda function:

    atmConsumerCase1Fn: Type: AWS::Serverless::Function Properties: CodeUri: atmConsumer/ Handler: handler.case1Handler Runtime: nodejs12.x
  2. Next, define the rule using an AWS::Events::Rule resource. The properties define the event pattern and can also specify targets. You can explicitly define multiple targets.

    EventRuleCase1: Type: AWS::Events::Rule Properties: Description: "Approved transactions" EventPattern: source: - "custom.myATMapp" detail-type: - transaction detail: result: - "approved" State: "ENABLED" Targets: - Arn: Fn::GetAtt: - "atmConsumerCase1Fn" - "Arn" Id: "atmConsumerTarget1"
  3. Finally, define an AWS::Lambda::Permission resource that grants permission to EventBridge to invoke the target.

    PermissionForEventsToInvokeLambda: Type: AWS::Lambda::Permission Properties: FunctionName: Ref: "atmConsumerCase1Fn" Action: "lambda:InvokeFunction" Principal: "events.amazonaws.com" SourceArn: Fn::GetAtt: - "EventRuleCase1" - "Arn"