-
Notifications
You must be signed in to change notification settings - Fork 0
/
rds-del-snap-region.js
63 lines (52 loc) · 2.03 KB
/
rds-del-snap-region.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
var AWS = require('aws-sdk');
AWS.config.update({
region: process.env.mainRegion
})
const { RDSFunc } = require('./rds-func')
const dateFunc = require('./date-func')
var rdsFunc = new RDSFunc(AWS)
module.exports.main = async (event) => {
var daysBefore = process.env.keepDays;
//RDS cluster
var cluster = await rdsFunc.describeClusters();
for (i=0;i<cluster.length;i++){
AWS.config.update({
region: process.env.copyRegion
})
var rdsFuncOtherRegion = new RDSFunc(AWS)
var snaps = await rdsFuncOtherRegion.describeClustersManualSnapshot(cluster[i].DBClusterIdentifier)
if(snaps){
snaps.forEach(async (snap)=>{
var copyDate = dateFunc.minusDaysFromToday(daysBefore);
var snapshotDate = dateFunc.removeTimeFromDate(snap.SnapshotCreateTime);
if (copyDate == snapshotDate) {
var copy = await rdsFuncOtherRegion.deleteClusterSnapshot(snap.DBClusterSnapshotIdentifier)
console.log(copy+" Rds cluster snapshot deleted")
}
})
}
}
AWS.config.update({
region: process.env.mainRegion
})
//RDS Instance
var instances = await rdsFunc.describeInstances();
for (i=0;i<instances.length;i++){
AWS.config.update({
region: process.env.copyRegion
})
var rdsFuncOtherRegion = new RDSFunc(AWS)
var snaps = await rdsFuncOtherRegion.describeInstanceManualSnapshot(instances[i].DBInstanceIdentifier)
if(snaps){
snaps.forEach(async (snap)=>{
var copyDate = dateFunc.minusDaysFromToday(daysBefore);
var snapshotDate = dateFunc.removeTimeFromDate(snap.SnapshotCreateTime);
if (copyDate == snapshotDate) {
var copy = await rdsFuncOtherRegion.deleteInstanceSnapshot(snap.DBSnapshotIdentifier)
console.log(copy+" Rds instance snapshot deleted")
}
})
}
}
return { message: 'RDS Snapshots successfully cloned!', event };
}