Created
June 25, 2015 07:42
-
-
Save giuliocalzolari/8fd70fe161035a4aaef3 to your computer and use it in GitHub Desktop.
AWS Lambda snapshot
This file contains hidden or 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
// createSnapshots - Create snapshots from a list of volumes | |
// Include the AWS SDK and set the region | |
var AWS = require('aws-sdk'); | |
AWS.config.region = 'us-east-1'; | |
// New EC2 object | |
var ec2 = new AWS.EC2(); | |
// Build a YYYYMMDD formatted current date string | |
var now = new Date(); | |
var year = now.getFullYear(); | |
var month = now.getMonth() + 1; | |
month = (month < 10 ? "0" : "") + month; | |
var day = now.getDate(); | |
day = (day < 10 ? "0" : "") + day; | |
now = year + month + day; | |
// List of volumes stored in an array of strings | |
var volumes = ['vol-ebde6bf0', | |
'vol-11454908', | |
'vol-7525b939', | |
'vol-6e26c322', | |
'vol-64b5b529', | |
'vol-4d2c3400' | |
]; | |
// Epmty array to hod the new snapshot IDs | |
var snapshots = []; | |
var i = 0; | |
exports.createSnapshots = function(event, context) { | |
for (index = 0; index < volumes.length; ++index) { | |
var params = { | |
VolumeId: volumes[index], | |
Description: now + " daily Snapshot for " + volumes[index], | |
DryRun: false | |
}; | |
ec2.createSnapshot(params, function(err, data) { | |
if (err) console.log(err, err.stack); // an error occurred | |
else { // successful response | |
console.log("Created snapshot request for %s", data.VolumeId); | |
console.log(data); | |
snapshots.push(data.SnapshotId); | |
if (i == (volumes.length - 1)) { | |
for (numsnaps = 0; numsnaps < snapshots.length; ++numsnaps) { | |
console.log("Snapshot %s pending", snapshots[numsnaps]); | |
} | |
context.done(); | |
} else ++i; | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment