How to use Header or Body based Routing within the API Gateway for SQS queue

  1. Using Only header based Routing Abilities within the API gateway — header_customheader-dev-swagger-apigateway.yaml
  2. Using Only body based Routing Abilities within the API gateway — body_customheader-dev-swagger-apigateway.yaml
  3. Use Lambda Proxy Integration. — Send the custom header sent with the request in the lambda event object. Now, custom code logic inside lambda function which is integration endpoint could be written to look for required header sent from API request and then send message to a particular SQS Queue.
header_customheader-dev-swagger-apigateway.yaml
---
swagger: "2.0"info:version: "2020-03-13T15:03:26Z"title: "CustomHeaderAPISQS"host: "0a7z8wycag.execute-api.us-east-1.amazonaws.com"basePath: "/dev"schemes:- "https"paths:/:post:produces:- "application/json"parameters:- name: "messageBody"in: "query"required: truetype: "string"- name: "tenantId"in: "header"required: truetype: "string"responses:200:description: "200 response"schema:$ref: "#/definitions/Empty"x-amazon-apigateway-integration:credentials: "<API_SQS_ROLE_ARN>"uri: "arn:aws:apigateway:us-east-1:sqs:path/<ACCOUNT_ID>/{tenantId}-apigw-queue"responses:default:statusCode: "200"requestParameters:integration.request.querystring.MessageBody: "method.request.querystring.messageBody"integration.request.querystring.Action: "'SendMessage'"integration.request.path.tenantId: "method.request.header.tenantId"integration.request.header.Content-Type: "'application/json'"passthroughBehavior: "when_no_match"httpMethod: "GET"type: "aws"definitions:Empty:type: "object"title: "Empty Schema"body_customheader-dev-swagger-apigateway.yaml
---
swagger: "2.0"info:version: "2020-03-14T00:03:33Z"title: "CustomBodyAPISQS"host: "e4d39swkil.execute-api.us-east-1.amazonaws.com"basePath: "/dev"schemes:- "https"paths:/:post:consumes:- "application/json"produces:- "application/json"responses:200:description: "200 response"schema:$ref: "#/definitions/Empty"x-amazon-apigateway-integration:credentials: "<API_SQS_ROLE_ARN>"uri: "arn:aws:apigateway:us-east-1:sqs:path/<ACCOUNT_ID>/{tenantId}-apigw-queue"responses:default:statusCode: "200"requestParameters:integration.request.path.bucket: "method.request.body.sqsName"integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"requestTemplates:application/json: "Action=SendMessage&MessageBody=$util.urlEncode($util.escapeJavaScript($input.json('$.Message')))\n"passthroughBehavior: "never"httpMethod: "POST"type: "aws"definitions:Empty:type: "object"title: "Empty Schema"

Prerequisites:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:PutLogEvents",
"logs:GetLogEvents",
"logs:FilterLogEvents"
],
"Resource": "*"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Resource": [
"YOUR-SQS-ARN"
],
"Action": [
"sqs:SendMessage",
"sqs:ReceiveMessage"
]
}
]
}
1. Replace "<API_SQS_ROLE_ARN>" with the Role ARN created in Step 12. Replace "<ACCOUNT_ID>" with your Account Account Id.3. Save it

Examples:

curl --location --request POST 'https://lxfsn87jd9.execute-api.us-east-1.amazonaws.com/dev?messageBody=%22HELLO-SQS%22' \--header 'tenantId: test2'

Using Body:

curl --location --request POST 'https://e4d39swkil.execute-api.us-east-1.amazonaws.com/dev' \
--data-raw '{
"sqsName" : "test2",
"Message" : "Postman says Hello"
}'

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How do you prepare for a Coding Bootcamp?

Microservices In Practice: Developing Instagram Clone —Auth Service

A Frontend Without a Backend

What is GraphQL Field monitoring

Python Programming Beginners Tutorial : Python 3 Programming | Free Course

“I’m feeling lucky” directly from the Chrome omni bar

Android app Development for Startups & Business in Sydney — Beedev Solutions

Create a Custom UIButton in Swift

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alien

Alien

More from Medium

Export data from DynamoDB

Google Cloud Functions with authorization headers

How To Send Transactional Emails Using Lambda and Amazon SES?

Upgrade Aurora PostgreSQL with Minimal Downtime using AWS DMS