Skip to content

Latest commit

 

History

History

apigw-eventbridge

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

AWS API Gateway to Amazon EventBridge

This pattern deploys an API Gateway HTTP API with a custom domain configuration and permissions to publish HTTP requests as events to EventBridge.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigateway-http-eventbridge-custom

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details.

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:

    git clone https://github.com/aws-samples/serverless-patterns
    
  2. Change directory to the pattern directory:

    cd apigw-eventbridge
    
  3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:

    sam deploy --guided --capabilities CAPABILITY_NAMED_IAM
    
  4. During the prompts:

    • Enter a stack name
    • Enter the desired AWS Region
    • Allow SAM CLI to create IAM roles with the required permissions.

    Once you have run sam deploy --guided mode once and saved arguments to a configuration file (samconfig.toml), you can use sam deploy in future to use these defaults.

  5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

How it works

The endpoint that will be created might look like, for example: http://dev-events.example.com/apigw2eb/{source}/{detailType}

Simply specify any source and detailType as a path parameters. The body of the request could be any valid json object.

The AWS SAM template deploys the following resources

Type Logical ID
AWS::ApiGatewayV2::Api HttpApi
AWS::Events::EventBus ApplicationEventBus
AWS::ApiGatewayV2::Stage HttpApiStage
AWS::ApiGatewayV2::ApiMapping HttpApiMapping
AWS::IAM::Role HttpApiIntegrationEventBridgeRole
AWS::ApiGatewayV2::Integration HttpApiIntegrationEventBridge
AWS::ApiGatewayV2::Route HttpApiRoute
AWS::CloudFormation::Stack1 apigw2eb-[STAGE]

When you send an HTTP POST request, the API Gateway publishes an event to the custom event bus in EventBridge.

Testing

Use your preferred terminal to send a http request.

curl --location --request POST 'https://dev-events.example.com/apigw2eb/mysource/mydetailtype' \
--header 'Content-Type: application/json' \
--data-raw '{
    "mybody": {
        "attr1": 1,
        "attr2": [1,2]
    }
}'

The response would be like:

{
    "Entries": [
        {
            "EventId": "1a15592f-87a0-e0d8-8e21-172e63c57212"
        }
    ],
    "FailedEntryCount": 0
}

This means your event was published successfuly.

So, the Lambda event for the request above will look like:

{
    "version": "0",
    "id": "1a15592f-87a0-e0d8-8e21-172e63c57594",
    "detail-type": "mydetailtype",
    "source": "com.mycompany.mysource",
    "account": "xxxxxxxxxx74",
    "time": "2021-04-03T14:45:32Z",
    "region": "eu-central-1",
    "resources": [],
    "detail": {
        "mybody": {
            "attr1": 1,
            "attr2": [1, 2]
        }
    }
}

Create your own either Lambda function or any other consumer for events you send with this API Gateway endpoint.

Cleanup

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

Additional resources


Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0