Move DynamoDB Table from Local to AWS Cloud
This blog shows how to move DynamoDB local table and data to the DynamoDB Web
Service, unlike SQL Database, you can use the backup file to restore to a
different environment. So far at this moment, I'm writing, have not found out
any other way to move Dynamo local to AWS Web service. So following sample
code will just loop each item and use aws-sdk
to put the request
to the AWS DynamoDB web service.
DynamoDB table schema
Following is the sample table schema, you can manually add the table on AWS Dynamo cloud or use SDK to create a table programmatically.
var params = {
TableName : 'MyFakeFriend',
KeySchema: [
{ AttributeName: "name", KeyType: "HASH" },
{ AttributeName: "age", KeyType: "RANGE" },
],
AttributeDefinitions: [
{ AttributeName: "name", AttributeType: "S" },
{ AttributeName: "age", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
Prepare the table data from local DynamoDB
This command will scan the entire table MyFakeFriend and output the result to the data.json file.
aws dynamodb scan --table-name MyFakeFriend --endpoint-url http://localhost:8000 > data.json
JSON data output
Now, the following is the result when I run the JSON output command, so I have 20 items need to add to the cloud Dynamo Service.
{
"Count": 20,
"Items": [
{
"phone": {
"S": "325-550-4097"
},
"age": {
"S": "16"
},
"name": {
"S": "Clifton Trantow"
}
},
{
"phone": {
"S": "923.425.3214 x763"
},
"age": {
"S": "39"
},
"name": {
"S": "Horace Luettgen"
}
}
],
"ScannedCount": 20,
"ConsumedCapacity": null
}
Put request to add data
Following is the sample code to loop through Dynamo local items and put the request to the cloud AWS Dynamo Service
var AWS = require("aws-sdk");
var fs = require('fs');
function addData() {
AWS.config.update({
region: "us-west-2",
endpoint: "https://dynamodb.us-west-2.amazonaws.com"
});
var dynamodb = new AWS.DynamoDB();
fs.readFile('./data.json', (err, data) => {
if (err) throw err;
let jsonData = JSON.parse(data);
jsonData.Items.forEach(item => {
var params = {
TableName: 'MyFakeFriend',
Item: item
}
dynamodb.putItem(params, function(err, data) {
if (err) {
console.error("Unable to add data", JSON.stringify(err, null, 2));
} else {
console.log("Able to add data", JSON.stringify(data, null, 2));
}
})
});
});
}
addData();