DynamoDB
The adapter to handle requests from AWS DynamoDB.
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 DynamoDB 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 { DynamoDBStreamEvent } from 'aws-lambda';
About the adapter
In AWS DynamoDB, you don't have requests, you just receive the records in the event
property of the handler.
So, in order to handle this adapter, by default we create a POST
request to /dynamo
with the body
being the event
property as JSON.
{
"Records": [
{
"eventID": "1",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"NewImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES",
"SequenceNumber": "111",
"SizeBytes": 26
},
"awsRegion": "us-west-2",
"eventName": "INSERT",
"eventSourceARN": "arn:aws:dynamodb:us-east-1:111122223333:table/EventSourceTable",
"eventSource": "aws:dynamodb"
}
]
}
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 dynamoDBForwardMethod
and dynamoDBForwardPath
inside DynamoDBAdapterOptions.
Usage
To add support to AWS DynamoDB you do the following:
import { ServerlessAdapter } from '@h4ad/serverless-adapter';
import { DynamoDBAdapter } 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 DynamoDBAdapter())
// customizing:
// .addAdapter(new DynamoDBAdapter({ dynamoDBForwardPath: '/prod/dynamo', dynamoDBForwardMethod: 'PUT' }))
.build();
When you configure your API with some basePath
like /prod
, you should set dynamoDBForwardPath
as /prod/dynamo
instead leave as default /dynamo
.
Security
You MUST check if the header Host
contains the value of dynamodb.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 /dynamo
.
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 DynamoDBAdapter({
batch: true,
});
And then, just return the following JSON in the route that processes the DynamoDB event.
{
"batchItemFailures": [
{
"itemIdentifier": "id2"
},
{
"itemIdentifier": "id4"
}
]
}