event-bridge.yaml
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: "AWS EventBridge Bus & API Gateway"

Parameters

These are the input parameters for this template. All of these parameters must be supplied for this template to be deployed.

Parameters:

The S3 bucket name where the Lambda functions live. This is used to create an IAM access policy to permit the application EC2 instances access to the S3 bucket.

ServiceName: Description: Name to be used for the EventBridge Bus and API Gateway, i.e. some-service-name Type: String Resources: EventBus: Type: AWS::Events::EventBus Properties: Name: !Ref ServiceName ApiGatewayApi: Type: AWS::Serverless::Api Properties: StageName: prod DefinitionBody: openapi: "3.0.1" info: title: !Ref ServiceName version: "2020-05-20T19:05:35Z" paths: /: post: responses: "200": description: "200 response" content: application/json: schema: $ref: "#/components/schemas/Empty" x-amazon-apigateway-integration: credentials: !GetAtt APIGatewayRole.Arn uri: "arn:aws:apigateway:us-west-2:events:action/PutEvents" responses: default: statusCode: "200" requestParameters: integration.request.header.X-Amz-Target: "'AWSEvents.PutEvents'" integration.request.header.Content-Type: "'application/x-amz-json-1.1'" requestTemplates: application/json: Fn::Sub: | #set($inputRoot = $input.path('$')) { "Entries": [ { "DetailType": "Event Type", "Source": "Event Source", "EventBusName": "${ServiceName}", "Detail": "$util.escapeJavaScript($input.json('$'))" } ] } passthroughBehavior: "when_no_templates" httpMethod: "ANY" type: "aws" components: schemas: Empty: title: "Empty Schema" type: "object" APIGatewayRole: Type: AWS::IAM::Role Properties: RoleName: !Sub "${ServiceName}-apigateway-role" AssumeRolePolicyDocument: Statement: - Effect: Allow Principal: Service: - apigateway.amazonaws.com Action: - sts:AssumeRole Policies: - PolicyName: EventBridgeAccess PolicyDocument: Statement: - Effect: Allow Action: - events:PutEvents Resource: '*' EventBusCatchallLogGroup: Type: "AWS::Logs::LogGroup" Properties: LogGroupName: !Sub "/aws/events/${ServiceName}" RetentionInDays: 3 CatchallRule: Type: AWS::Events::Rule Properties: Description: "Default Catch-All EventRule" EventBusName: !Ref EventBus EventPattern: account: - !Ref AWS::AccountId State: "ENABLED" Targets: - Arn: !GetAtt EventBusCatchallLogGroup.Arn Id: !Sub "${ServiceName}-log-group" Outputs: RESTEndpoint: Value: !Sub "https://${ApiGatewayApi}.execute-api.us-west-2.amazonaws.com/prod/" Export: Name: !Sub "${AWS::StackName}-rest-endpoint"