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.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
-
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
-
Change directory to the pattern directory:
cd apigw-eventbridge
-
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
-
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 usesam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
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.
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.
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.
- Delete the stack
aws cloudformation delete-stack --stack-name STACK_NAME
- Confirm the stack has been deleted
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0