REST API with SQS Integration

Implement API Gateway integration with SQS.

API to send message

Amazon Simple Queue Service (SQS) is a popular messaging service from AWS. Integration using a queue service is ideal for fire-and-forget APIs where resilient eventual consistency is more important than instant response.

SQS integrates well with the API Gateway. So, we can send messages on the SQS directly from the API gateway. We can have another process elsewhere that reads and consumes these messages. The sender API can send without worrying about the consumer. That’s the advantage of using SQS-based integration.

Implementation

The API for SQS is based on path and query string parameters. The action as well as the data go into the query string parameters. The details about the target queue go into the path. So, for a SendMessage, the authorized API calls into SQS like this: {baseurl}/{AccountID}/{QueueName}?Action=SendMessage&Body=Payload. We should configure the API Gateway to map the input data into the target URL's query string. Our API can get this information in any form. It could be in the body, path parameters, or query string. However, we should map it so that the target API call carries the data in query string parameters.

Let’s check out how this works. The code below defines two integrations for the SQS. It creates one API with two methods that map to SendMessage and ReceiveMessage. We can have two independent clients that use the API to send and consume messages.

Get hands-on with 1400+ tech skills courses.