/
snapshot-cleanup.js
54 lines (46 loc) · 1.31 KB
/
snapshot-cleanup.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
#!/usr/bin/env node
var url = require('url');
var AWS = require('aws-sdk');
var _ = require('lodash');
// Configure AWS region
AWS.config.region = 'us-east-1';
// Instantiate ec2 w/ specific API version
var ec2 = new AWS.EC2({apiVersion: '2015-10-01'});
var deleteSnapshot = function(snapshot_id) {
var snapshot_params = {
SnapshotId: snapshot_id,
DryRun: false
};
ec2.deleteSnapshot(snapshot_params, function(err, data) {
if (err)
console.log(err, err.stack);
else {
console.log("Successfully deleted " + snapshot_id);
}
});
}
// Query EBS volumes tagged "backup"
var params = {
DryRun: false,
Filters: [
{
Name: 'tag-key',
Values: [
'Retention'
]
}
]
};
var getExpired = function(snapshot) {
// Check if Date Created + Retention < Date Created
var retention = _.map(snapshot.Tags, "Value")[0] // Used _.map in case there are other Tags
return new Date(new Date(snapshot.StartTime).getTime() + (parseInt(retention) * 3600 * 1000)) < new Date()
}
ec2.describeSnapshots(params, function(err, data) {
if (err)
console.log(err, err.stack);
else
var expired_snapshots = _.filter(data.Snapshots, getExpired);
var expired_snapshot_ids = _.map(expired_snapshots, 'SnapshotId')
_.map(expired_snapshot_ids, deleteSnapshot)
});