-
Notifications
You must be signed in to change notification settings - Fork 1
/
Delete_AMI.js
81 lines (71 loc) · 2.47 KB
/
Delete_AMI.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
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);
});
}
}