-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (59 loc) · 2.08 KB
/
index.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
const AWS = require("aws-sdk");
AWS.config.update({ region: 'eu-west-1' });
const _ = require("underscore");
const moment = require("moment");
const dynamodb = new AWS.DynamoDB({
apiVersion: '2012-08-10'
});
const ENV = process.env.ENV;
const BACKUPS_TO_KEEP = process.env.BACKUPS_TO_KEEP;
exports.handler = async (event, context, callback) => {
(await getTables(ENV)).forEach((tableName) => {
dynamodb.listBackups({
TableName: tableName
}, (err, data) => {
if (err) {
console.log(err);
} else {
let backups = _.sortBy(data.BackupSummaries, 'BackupCreationDateTime');
// Remove the backups we DON'T want to delete.
let backupsToRemove = BACKUPS_TO_KEEP - 1;
backups.splice(-backupsToRemove, backupsToRemove);
// Delete remaining backups
backups.forEach((backup) => {
dynamodb.deleteBackup({
BackupArn: backup.BackupArn
}, (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
});
dynamodb.createBackup({
TableName: tableName,
BackupName: tableName + '_' + moment().format('YYYYMMDD_HHmmss')
}, (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
}
});
});
callback(null, "Execution Successful");
};
var getTables = async (env) => {
try {
var tempTableList = (await dynamodb.listTables({}).promise()).TableNames;
return tempTableList.filter(item => String(item).startsWith(env));
} catch (error) {
return {
statusCode: 400,
error: `Could not post: ${error.stack}`
};
}
};