Last active
February 20, 2021 18:00
-
-
Save vmuneeb/1c0a447fb93baa9500bfa4b6dad1e694 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source https://tvernon.tech/blog/move-dynamodb-data-between-regions | |
const AWS = require('aws-sdk') | |
// Use the scan method to get everything from the old table | |
const readAllDataFromTable = async ({ region, table }) => { | |
AWS.config.update({ region }) | |
const documentClient = new AWS.DynamoDB.DocumentClient() | |
const params = { | |
TableName: table, | |
}; | |
let scanResults = []; | |
let items; | |
do { | |
items = await documentClient.scan(params).promise(); | |
items.Items.forEach((item) => scanResults.push(item)); | |
params.ExclusiveStartKey = items.LastEvaluatedKey; | |
} while (typeof items.LastEvaluatedKey != "undefined"); | |
console.log(scanResults.length) | |
return scanResults; | |
} | |
// Write one row of data to the new table | |
const writeRowToTable = async (db, table, row) => { | |
return await new Promise((resolve, reject) => { | |
db.put( | |
{ | |
TableName: table, | |
Item: row, | |
}, | |
err => { | |
if (err) { | |
reject() | |
} else { | |
resolve() | |
} | |
} | |
) | |
}) | |
} | |
// Write all the data to the new table | |
const writeDataToTable = async ({ region, table, data }) => { | |
AWS.config.update({ region }) | |
const db = new AWS.DynamoDB.DocumentClient() | |
// Keep a count of the successful writes so we can know if | |
// all the items were written successfully | |
let successfulWrites = 0 | |
await Promise.all( | |
data.map(async item => { | |
return new Promise(async resolve => { | |
try { | |
await writeRowToTable(db, table, item) | |
successfulWrites++ | |
} catch (e) { | |
// If something fails, log it | |
console.log('error', e) | |
} | |
resolve() | |
}) | |
}) | |
) | |
console.log(`wrote ${successfulWrites} of ${data.length} rows to database`) | |
} | |
// Run the script | |
;(async function() { | |
// Store all the data in memory to write later | |
const data = await readAllDataFromTable({ | |
region: 'us-east-1', | |
table: OLD_TABLE_NAME, | |
}) | |
// Write the saved data to the new table | |
await writeDataToTable({ | |
region: 'ap-south-1', | |
table: NEW_TABLE_NAME, | |
data, | |
}) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment