DynamoDB Replicator
Lambda-ready npm package for replicating DynamoDB tables within or between regions.
Supports one-way replication (single master to slave(s))
Usage
Prerequisites
You'll need NodeJS and NPM installed on your local machine.
Prepare your DynamoDB table
Add a stream to the DynamoDB table you want to use as your master.
- In the Overview tab of the DynamoDB table in the AWS console click Manage Stream.
- In the overlay that pops up choose New Image (although New and old images should also work should you need that for other reasons).
- Click Enable.
Alternatively, if you want to provision your DynamoDB table with CloudFormation add the StreamSpecification
property to the AWS::DynamoDB::Table
object, e.g.
Create the replication Lambda function
- Create a new empty directory on your computer
- Within that folder create two files, one called
package.json
and another calledindex.js
. - Edit
package.json
and save{}
as the contents of that file. - In your terminal type
npm install --save dynamo-replicator es6-promise
- Edit
index.js
and save the contents of the file shown below (changingSLAVE-REGION
andSLAVE-TABLE
to be the region and table name of the DynamoDB to be replicated to). - zip the contents of the folder up.
- Create a new Lambda function with a role that has sufficient IAM permissions (as a minimum it needs
dynamodb::PutItem
anddynamodb::DeleteItem
on the slave table. - On the Triggers tab of your master DynamoDB, click Create Trigger and New Function; for the Configure event source options, ensure the DynamoDB table is your master table and leave everything else as default and click Next; for the Configure function options name your function something like
dynamodb-replicator
and for Code entry type select Upload a .ZIP file and choose the zip file created earlier. - Repeat the previous step for multiple slaves changing
SLAVE-REGION
andSLAVE-TABLE
inindex.js
each time and re-zipping for each slave. - Test by creating and deleting items from the master table and ensuring that the slaves update accordingly.
Contents of index.json
:
'use strict'; ; var replicator = region: 'SLAVE-REGION' table: 'SLAVE-TABLE'; exports { replicator ;};