Skip to content

Instantly share code, notes, and snippets.

@thepont
Created November 21, 2017 03:47
Show Gist options
  • Save thepont/b6ae8b89985dd750a398c1e114619e68 to your computer and use it in GitHub Desktop.
Save thepont/b6ae8b89985dd750a398c1e114619e68 to your computer and use it in GitHub Desktop.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment