Skip to content

Instantly share code, notes, and snippets.

@giuliocalzolari
Created June 25, 2015 07:42
Show Gist options
  • Save giuliocalzolari/8fd70fe161035a4aaef3 to your computer and use it in GitHub Desktop.
Save giuliocalzolari/8fd70fe161035a4aaef3 to your computer and use it in GitHub Desktop.
AWS Lambda snapshot
// 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