Majestic.cloud

Integrating AWS API Gateway with SQS

API Gateway integration with SQS - implementing the Queue-based Load Leveling Pattern

Implementing the Queue-Based Load Leveling pattern

In some cases you might want to process the requests that are coming into API Gateway in an asynchronous way. That could be happening for many reasons but usually you might want to do this if you want to avoid overwhelming some downstream integration.

Let’s imagine a scenario where you have an e-commerce website that receives a large number of requests to place orders during peak traffic periods. If your backend is unable to handle the requests you are losing orders. So you would need to find a way to handle these orders in an asynchronous way, allowing your workers to handle the orders later.

Now even if you would have Lambda functions as integration points for your API Gateway you could still run into issues when Lambda functions are unable to handle the requests due to various factors. For example you could run into the account concurrency limit and then your Lambda functions would get throttled. Or it could happen that you have such a massive burst of traffic that even if your concurrency quotas are not a problem you could run into the burst limits for Lambda in a particular region (which cannot be modified).

So if your application can handle an asynchronous way of operating you can implement the Queue-Based Load Leveling pattern where you add queue as integration for your API Gateway method and then your workers (which could be Lambdas or other services) can pick up the messages from the queue and process them. Your queue will act as a buffer in this case.

If you work with AWS then SQS or the Simple Queue Service is the natural choice for queues as it is both a highly available and scalable service and it is actually supported by the API Gateway out of the box.

In this case the flow of data would look like in the diagram below:

Some of the benefits in this case would be:

Implementation

So let’s implement it below (at the end of the article there is also a video guiding you how to do it).

1. Create an SQS queue

First we will create the SQS queue that we are going to use, as we need the URL and the ARN of this queue for the next steps.

2. Create an IAM policy

Then we need to create an IAM policy and then a Role, this will define the permissions, we are going to need this for our API Gateway to send messages to the Queue

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sqs:SendMessage",
            "Resource": "SQS_QUEUE_ARN"
        }
    ]
}

3. Create an IAM role

4. Create API Gateway and configure it

5. Deploy and test the API

curl --location --request POST 'https://API_ID.execute-api.REGION.amazonaws.com/STAGE/RESOURCE' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "message": "This is the message that should be sent to SQS"
    }'

You should be getting back something like this:

{
   "SendMessageResponse":{
      "ResponseMetadata":{
         "RequestId":"38bcbc0d-dec1-5971-a653-e04560d73ece"
      },
      "SendMessageResult":{
         "MD5OfMessageAttributes":null,
         "MD5OfMessageBody":"d117718ecf5e1016a6d35ee71b7a3c69",
         "MD5OfMessageSystemAttributes":null,
         "MessageId":"e3880f14-80f6-47bb-a45b-b69c8cddc887",
         "SequenceNumber":null
      }
   }
}

So that’s about it. Now you should have a working integration between API Gateway and SQS and your incoming requests will be sent to SQS from where you will be able to process them in an asynchronous manner.

Here is the walkthrough in video format if you find that easier to follow:

Exit mobile version