Skip to content

Instantly share code, notes, and snippets.

@singledigit
Created August 18, 2020 22:06
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save singledigit/9cd1bbce1332aaad479d425641cc363f to your computer and use it in GitHub Desktop.
Creating an EventBridge rule with API Gateway as the target in AWS SAM
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Event Bridge -> API Gateway
Resources:
RestApiGateway:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
# Function here for the route and testing
RestApiFunction:
Type: AWS::Serverless::Function
Properties:
InlineCode: |
exports.handler = async (event) => {
console.log(JSON.stringify(event))
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
Handler: index.handler
Runtime: nodejs12.x
Events:
FetchRest:
Type: Api
Properties:
RestApiId: !Ref RestApiGateway
Method: POST
Path: /
### API Gateway ARNs: https://docs.aws.amazon.com/apigateway/latest/developerguide/arn-format-reference.html
### More on targets: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html
### More on PutTargets: https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutTargets.html
MyBusRule:
Type: AWS::Events::Rule
Properties:
Description: Event bridge rule to trigger REST API
EventPattern: { "source": [ "myBusEvent" ] }
Targets:
- Arn: !Sub arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${RestApiGateway}/Prod/POST/*
Id: MyRestTarget
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment