Skip to main content

SQS

The adapter to handle requests from AWS SQS.

info

The option of responseWithErrors is ignored by this adapter and we always call resolver.fail with the error.

Typescript

To correctly type your body when receiving the AWS SQS request, you must install aws-lambda:

npm i --save-dev @types/aws-lambda

So when getting the body you should use this type:

sqs.controller.ts
import type { SQSEvent } from 'aws-lambda';

About the adapter

In AWS SQS, you don't have requests, you just receive the records from the queue in the event property of the handler.

So, in order to handle this adapter, by default we create a POST request to /sqs with the body being the event property as JSON.

sqs-event-example.json
{
"Records": [
{
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
"body": "Test message.",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1545082649183",
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
"ApproximateFirstReceiveTimestamp": "1545082649185"
},
"messageAttributes": {},
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
"awsRegion": "us-east-2"
}
]
}

Normally, your framework will parse this JSON and return the parsed values as javascript objects.

Customizing

You can change the HTTP Method and Path that will be used to create the request by sending sqsForwardMethod and sqsForwardPath inside SQSAdapterOptions.

Usage

To add support to AWS SQS you do the following:

index.ts
import { ServerlessAdapter } from '@h4ad/serverless-adapter';
import { SQSAdapter } from '@h4ad/serverless-adapter/adapters/aws';
import { DefaultHandler } from '@h4ad/serverless-adapter/handlers/default';
import app from './app';

export const handler = ServerlessAdapter.new(app)
.setHandler(new DefaultHandler())
// .setFramework(new ExpressFramework())
// .setResolver(new PromiseResolver())
.addAdapter(new SQSAdapter())
// customizing:
// .addAdapter(new SQSAdapter({ sqsForwardPath: '/prod/sqs', sqsForwardMethod: 'PUT' }))
.build();
caution

When you configure your API with some basePath like /prod, you should set sqsForwardPath as /prod/sqs instead leave as default /sqs.

Security

You MUST check if the header Host contains the value of sqs.amazonaws.com.

Without checking this header, if you add this adapter and AWS API Gateway V2 adapter, you will be vulnerable to attacks because anyone can create a POST request to /sqs.

What happens when my response status is different from 2xx or 3xx?

Well, this library will throw an error. In previous versions of this library, the behavior was different, but now we throw an error if the status does not indicate success.

When it throws an error, the request will simply fail to process the event, and depending on how you set up your dead-letter queue or your retry police, can be sent to dead-letter queue for you to check what happens or try again.

Batch Item Failures

If you enable this batch item failure option, to be able to partially return that some items failed to process, first configure your Adapter:

const adapter = new SQSAdapter({
batch: true,
});

And then, just return the following JSON in the route that processes the SQS event.

{
"batchItemFailures": [
{
"itemIdentifier": "id2"
},
{
"itemIdentifier": "id4"
}
]
}

Reference