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

Vipul Munot
2 min readMar 18, 2020

--

Currently, The API Gateway does not have header or body based Routing capabilities.

Use case

Depending upon header or body input send message to appropriate SQS queues.

For example:

Lets say you have different vendors A,B,C and you want to send the Messages to different queue A-apigw-queue,B-apigw-queue and C-apigw-queue.

Possible workaround:

  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.

For Options 1 and 2:

Files:

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:

1. All queue must be already created A-apigw-queue,B-apigw-queue and C-apigw-queue.

Step 1:

Create a Role with following policies:

{
"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"
]
}
]
}

Step 2:

Open the Swagger yaml template:

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

Step 3: Import the Yaml File in API Gateway and deploy the API.

Examples:

Using Header

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"
}'

--

--

No responses yet