-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-lambda-delete-snapshots.js
89 lines (85 loc) · 2.59 KB
/
aws-lambda-delete-snapshots.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
// Include AWS EC2
const AWS = require( "aws-sdk" );
const EC2 = new AWS.EC2();
// Function to check if snapshot is older than 3 days
function is_older_than_3_days( snapshot_date ) {
// Pass in the snapshot date from the loop in the main entrypoint function
if ( typeof snapshot_date === "undefined" || typeof snapshot_date === "null" ) {
return false;
}
if ( snapshot_date === "" ) {
return false;
}
// Define a new date class with the snapshot date
const snapshot_start = new Date( snapshot_date ).getTime();
// Start with now, get 3 days ago, then finish off with converting to milliseconds
const three_days_ago = new Date( new Date().setDate( new Date().getDate() - 3 ) ).getTime();
// If snapshot start date is less than 3 days ago, return true or return false otherwise
if ( snapshot_start < three_days_ago ) {
return true;
} else {
return false;
}
}
// Function to handle the deletion
function snapshot_delete( snapshot_id ) {
// Pass in the snapshot ID from the loop in the main entrypoint function
if ( typeof snapshot_id === "undefined" || typeof snapshot_id === "null" ) {
return false;
}
if ( snapshot_id === "" ) {
return false;
}
// Define some request params
const params = {
SnapshotId: snapshot_id
};
EC2.deleteSnapshot( params, function( err, data ) {
if ( err ) {
console.log( err, err.stack );
} else {
console.log( `Successfully deleted: ${snapshot_id}.` );
}
});
}
// Our main function to list snapshots, the entrypoint
function listSnapshots() {
console.log( "Listing snapshots..." );
// Grab env stored in Lambda
const volume_id = process.env.VOLUME_ID;
if ( typeof volume_id === "undefined" || typeof volume_id === "null" ) {
console.log( "Null params." );
return false;
}
if ( volume_id === "" ) {
console.log( "Empty params." );
return false;
}
// Define some request params
const snapshotsParams = {
Filters: [{
Name: "status",
Values: [ "completed" ],
Name: "volume-id",
Values: [ volume_id ]
}],
OwnerIds: [ "self" ]
};
EC2.describeSnapshots( snapshotsParams, function( err, data ) {
if ( err ) {
console.log( err, err.stack );
} else {
console.log( `There are currently ${data.Snapshots.length} snapshots.` );
let snapshot;
for ( snapshot of data.Snapshots ) {
if ( true === is_older_than_3_days( snapshot.StartTime ) ) {
console.log( `The snapshot: ${ snapshot.SnapshotId } is older than 3 days.` );
snapshot_delete( snapshot.SnapshotId );
} else {
console.log( `The snapshot: ${ snapshot.SnapshotId } is newer than 3 days.` );
}
}
}
});
}
exports.listSnapshots = listSnapshots;