Found a great little util for backing up dynamodb to s3 from dynamodb streams, dynamodb-backup-restore.
It's designed to store each event to an S3 bucket and allow you to restore your database from the list of events.
You can add it to your serverless.yml
and create a simple function.
const Backup = require('dynamodb-backup-restore').Backup;
module.exports.handler = (event, context, callback) => {
if (!event.Records) {
callback('There are no items to process.');
}
else {
let config = {
S3Bucket: process.env.S3_BACKUP_BUCKET,
S3Region: process.env.S3_REGION,
S3Prefix: process.env.S3_BACKUP_PREFIX
};
let backup = new Backup(config);
backup.fromDbStream(event.Records).then(() => {
callback();
}).catch(err => {
callback(err);
});
}
}
provider:
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:PutObject"
Resource:
- "arn:aws:s3:::${env:S3_BACKUP_BUCKET}"
- "arn:aws:s3:::${env:S3_BACKUP_BUCKET}/*"
# Etc....
functions:
backup:
handler: db-backup.handler
timeout: 300
memorySize: 256
events:
- stream:
type: dynamodb
arn:
Fn::GetAtt:
- EventLog #Name of your DB in your resources.
- StreamArn
batchSize: 1