SNS
The adapter to handle requests from AWS SNS.
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 SNS request, you must install aws-lambda
:
npm i --save-dev @types/aws-lambda
So when getting the body
you should use this type:
import type { SNSEvent } from 'aws-lambda';
About the adapter
In AWS SNS, you don't have requests, you just receive the records in the event
property of the handler.
So, to be able to handle this adapter, by default we create a POST
request to /sns
with the body
being the event
property as JSON.
{
"Records": [
{
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-2:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486",
"EventSource": "aws:sns",
"Sns": {
"SignatureVersion": "1",
"Timestamp": "2019-01-02T12:45:07.000Z",
"Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==",
"SigningCertUrl": "https://sns.us-east-2.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "Hello from SNS!",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
},
"Type": "Notification",
"UnsubscribeUrl": "https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-2:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486",
"TopicArn":"arn:aws:sns:us-east-2:123456789012:sns-lambda",
"Subject": "TestInvoke"
}
}
]
}
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 snsForwardMethod
and snsForwardPath
inside SNSAdapterOptions.
Usage
To add support to AWS SNS you do the following:
import { ServerlessAdapter } from '@h4ad/serverless-adapter';
import { SNSAdapter } 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 SNSAdapter())
// customizing:
// .addAdapter(new SNSAdapter({ snsForwardMethod: '/prod/sns', snsForwardPath: 'PUT' }))
.build();
When you configure your API with some basePath
like /prod
, you should set snsForwardPath
as /prod/sns
instead leave as default /sns
.
Security
You MUST check if the header Host
contains the value of sns.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 /sns
.
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.