Created
April 1, 2020 23:55
-
-
Save etc-tiago/2c7800b691603ad6e0b7cd48c8a857bd to your computer and use it in GitHub Desktop.
Create dynamodb backup with lambda
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
const AWS = require("aws-sdk"); | |
AWS.config.update({ | |
region: process.env.AWS_REGION || 'sa-east-1' | |
}); | |
const DynamoDB = new AWS.DynamoDB(); | |
const getListTables = () => { | |
return new Promise((resolve, reject) => { | |
try { | |
DynamoDB.listTables((err, data) => { | |
if (err) reject(err); | |
else resolve(data.TableNames); | |
}); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
}; | |
const createBackup = tableName => { | |
return new Promise((resolve, reject) => { | |
try { | |
let date = new Date(); | |
let timestamp = date.getFullYear().toString() + '-' + | |
date.getMonth().toString() + '-' + | |
date.getDay().toString() + '_' + | |
date.getHours().toString() + '-' + | |
date.getMinutes().toString(); | |
DynamoDB.createBackup({ | |
TableName: tableName, | |
BackupName: tableName + '_lambda-backup_' + timestamp | |
}, (err, data) => { | |
if (err) reject(err); | |
else resolve(data); | |
}); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
}; | |
module.exports.handler = async(event, context, callback) => { | |
const list = await getListTables(); | |
let backupCreated = []; | |
console.info('Serao ' + list.length.toString() + ' tabelas'); | |
list.forEach(async tableName => { | |
try { | |
let create = await createBackup(tableName); | |
console.info('Backup da Tabela ' + create.BackupDetails.BackupName + ' criado.'); | |
backupCreated.push(create.BackupDetails.BackupName); | |
} catch (error) { | |
console.error(error); | |
} | |
}); | |
callback(null, "done"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment