Skip to content

Instantly share code, notes, and snippets.

@marinr
Created August 24, 2020 18:25
Show Gist options
  • Save marinr/4e4ae76abddbd63b96b8004e970a49a0 to your computer and use it in GitHub Desktop.
Save marinr/4e4ae76abddbd63b96b8004e970a49a0 to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Example Event API EventBridge
Parameters:
Environment:
Default: 'Dev'
Type: String
Resources:
ExampleEventBus:
Type: AWS::Events::EventBus
Properties:
Name: exampleEventBus
ExampleEventProcessingAPI:
Type: "AWS::Serverless::Api"
Properties:
StageName: !Sub "${Environment}"
DefinitionBody:
swagger: "2.0"
info:
title: !Sub "${AWS::StackName}-api"
paths:
/process:
post:
responses:
"202":
description: Accepted
x-amazon-apigateway-integration:
type: "aws"
httpMethod: "POST"
uri: !Sub "arn:aws:apigateway:eu-west-1:events:path/event-bus"
credentials: !GetAtt EventsAPIRole.Arn
passthroughBehavior: "NEVER"
requestTemplates:
application/json:
|-
#set($msgBody = $input.body)
#set($context.requestOverride.header['X-Amz-Target'] = "AWSEvents.PutEvents")
#set($context.requestOverride.header['Content-Type'] = "application/x-amz-json-1.1")
{
"Entries": [
{
"Source":"com.alite.example",
"Detail": "$util.escapeJavaScript($msgBody).replaceAll("\\'","'")",
"DetailType": "ExampleEventsDetailType",
"EventBusName": "exampleEventBus"
}
]
}
responses:
default:
statusCode: 202
Example1Function:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
InlineCode: |-
exports.handler = function(event, context) {
console.log(JSON.stringify(event));
context.succeed('Processed');
};
Events:
MyCoolEvent:
Type: CloudWatchEvent
Properties:
EventBusName: exampleEventBus
Pattern:
detail:
type:
- my::cool::event1
- my::cool::event
Example2Function:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
InlineCode: |-
exports.handler = function(event, context) {
console.log(JSON.stringify(event));
context.succeed('Processed');
};
Events:
MyCoolEvent:
Type: CloudWatchEvent
Properties:
EventBusName: exampleEventBus
Pattern:
detail:
type:
- my::cool::event2
- my::cool::event
EventsAPIRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "apigateway.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: !Sub "${AWS::StackName}-events-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action: "events:PutEvents"
Effect: "Allow"
Resource: !GetAtt ExampleEventBus.Arn
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
EventsTopic:
Type: "AWS::SNS::Topic"
Properties:
TopicName: !Sub "Example-events-topic"
Event3Rule:
Type: AWS::Events::Rule
Properties:
Description: "EventSNSRule"
EventBusName: exampleEventBus
EventPattern:
detail:
type:
- my::cool::event3
State: "ENABLED"
Targets:
-
Arn:
!Ref EventsTopic
Id: "Event3Topic"
EventTopicPolicy:
Type: 'AWS::SNS::TopicPolicy'
Properties:
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: 'sns:Publish'
Resource: '*'
Topics:
- !Ref EventsTopic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment