Скрипт Aws lambda auto AMI для резервного копирования в облачном журнале по всему региону в
2 лямбда-функции для создания и удаления, и вам нужно создать новую политику и роль для них
creat_backup
var aws = require('aws-sdk');
Region = ['ap-south-1','eu-central-1','us-east-1'];
var now = new Date();
date = now.toISOString().substring(0, 10)
hours = now.getHours()
minutes = now.getMinutes()
exports.handler = function (event, context)
{
var instanceparams = {
Filters: [{
Name: 'tag:Backup',
Values: [
'yes'
]
}]
}
region(0);
async function region(h){
if(h>=Region.length)
return;
console.log("H Value Test",h);
aws.config.region = Region[h];
var ec2 = new aws.EC2();
console.log("SELECTED REGION",Region[h])
return await ec2.describeInstances(instanceparams, function(err, data) {
if (err) console.log(err, err.stack);
else {
for (var i in data.Reservations) {
var ec1 = new aws.EC2();
for (var j in data.Reservations[i].Instances) {
console.log("instance is ",data.Reservations[i].Instances[j].InstanceId);
instanceid = data.Reservations[i].Instances[j].InstanceId;
nametag = data.Reservations[i].Instances[j].Tags
for (var k in data.Reservations[i].Instances[j].Tags) {
if (data.Reservations[i].Instances[j].Tags[k].Key == 'Name') {
name = data.Reservations[i].Instances[j].Tags[k].Value;
}
}
console.log("Creating AMIs of the Instance: ", name);
var imageparams = {
InstanceId: instanceid,
Name: name + "_" + date + "_" + hours + "-" + minutes,
NoReboot: true
}
ec1.createImage(imageparams, function(err, data) {
if (err) console.log(err, err.stack);
else {
image = data.ImageId;
console.log("image",image);
var tagparams = {
Resources: [image],
Tags: [{
Key: 'DeleteOn',
Value: 'yes'
}]
};
ec1.createTags(tagparams, function(err, data) {
console.log("Tags added to the created AMIs");
});
}ec1=null;
});
}
}
}
aws.config.region = null;
ec2 = null;
h+=1
region(h)
});
}
}
функция удаления
var aws = require('aws-sdk');
Region = ['ap-south-1','eu-central-1','us-east-1'];
var d = new Date();
var x = 1; /* ------Retention Days------- */
d.setDate(d.getDate() - x);
reqdate = d.toISOString().substring(0, 10);
exports.handler = function(event, context) {
var instanceparams = {
Owners: [
'self'
],
Filters: [{
Name: 'tag:DeleteOn',
Values: [
'yes'
]
}]
}
region(0);
function region(h){
if(h>=Region.length)
return;
console.log("H Value Test",h);
aws.config.region = Region[h];
var ec2 = new aws.EC2();
console.log("SELECTED REGION",Region[h]);
ec2.describeImages(instanceparams, function(err, data) {
if (err) console.log(err, err.stack);
else {
for (var j in data.Images) {
imagename = data.Images[j].Name
imageid = data.Images[j].ImageId
//if (imagename.indexOf(reqdate) > -1) {
console.log("image that is going to be deregistered: ", imagename);
console.log("image id: ", imageid);
var deregisterparams = {
ImageId: imageid
};
ec2.deregisterImage(deregisterparams, function(err, data01) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log("Image Deregistered");
}
});
//}
}
setTimeout(function() {
for (var j in data.Images) {
imagename = data.Images[j].Name
// if (imagename.indexOf(reqdate) > -1) {
for (var k in data.Images[j].BlockDeviceMappings) {
snap = data.Images[j].BlockDeviceMappings[k].Ebs.SnapshotId;
console.log(snap);
var snapparams = {
SnapshotId: snap
};
ec2.deleteSnapshot(snapparams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log("Snapshot Deleted"); // successful response
});
}
//}
}
}, 30000);
}
aws.config.region = null;
h+=1
region(h);
});
}
}
для получения дополнительной информации посетите
https://github.com/harsh4870/AWS-auto-ami-backup-across-all-region