-
Notifications
You must be signed in to change notification settings - Fork 51
/
createAMI.js
96 lines (91 loc) · 4.49 KB
/
createAMI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Init AWS
var aws = require('aws-sdk');
var ec2 = new aws.EC2();
// OPTION - limit by region
// aws.config.region = 'eu-west-1';
// Variables for the script
// Changes below are not required but if you do change, then change to match delete and create lambda scripts.
const keyForEpochMakerinAMI = "DATETODEL-";
const keyForInstanceTagToBackup = "Backup"; // looks for string yes
const keyForInstanceTagDurationBackup = "BackupRetentionDays"; // accepts numbers like 5 or 10 or 22 and so on.
const keyForInstanceTagNoReboot = "BackupNoReboot"; // if true then it wont reboot. If not present or set to false then it will reboot.
// Lambda handler
exports.handler = function(event, context) {
var instanceparams = {
Filters: [
{
Name: 'tag:' + keyForInstanceTagToBackup,
Values: [
'yes'
]
},
/*
// OPTION - Add additional filters
{
Name: 'tag:someOtherTagName',
Values: [
'someValue'
]
}
*/
]
};
ec2.describeInstances(instanceparams, function(err, data) {
if (err) console.log(err, err.stack);
else {
for (var i in data.Reservations) {
for (var j in data.Reservations[i].Instances) {
var instanceid = data.Reservations[i].Instances[j].InstanceId;
var name = "", backupRetentionDaysforAMI = -1, backupRunTodayCheck = "", noReboot = false;
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;
}
if(data.Reservations[i].Instances[j].Tags[k].Key == keyForInstanceTagDurationBackup){
backupRetentionDaysforAMI = parseInt(data.Reservations[i].Instances[j].Tags[k].Value);
}
if(data.Reservations[i].Instances[j].Tags[k].Key == keyForInstanceTagNoReboot){
if(data.Reservations[i].Instances[j].Tags[k].Value == "true"){
noReboot = true;
}
}
}
// cant find when to delete then dont proceed.
if(backupRetentionDaysforAMI < 1){
console.log("Skipping instance Name: " + name + " backupRetentionDaysforAMI: " + backupRetentionDaysforAMI + " (backupRetentionDaysforAMI > 0)" + (backupRetentionDaysforAMI > 0));
}else{
console.log("Processing instance Name: " + name + " backupRetentionDaysforAMI: " + backupRetentionDaysforAMI + " (backupRetentionDaysforAMI > 0)" + (backupRetentionDaysforAMI > 0));
var genDate = new Date();
genDate.setDate(genDate.getDate() + backupRetentionDaysforAMI); // days that are required to be held
var imageparams = {
InstanceId: instanceid,
Name: name + "_" + keyForEpochMakerinAMI + genDate.getTime(),
};
if(noReboot == true){
imageparams["NoReboot"] = true;
}
console.log(imageparams);
ec2.createImage(imageparams, function(err, data) {
if (err) console.log(err, err.stack);
else {
var image = data.ImageId;
console.log(image);
var tagparams = {
Resources: [image],
Tags: [{
Key: 'DeleteBackupsAutoOn',
Value: 'yes'
}]
};
ec2.createTags(tagparams, function(err, data) {
if (err) console.log(err, err.stack);
else console.log("Tags added to the created AMIs");
});
}
});
}
}
}
}
});
}