Created
August 12, 2018 05:53
-
-
Save pratheekhegde/9e7cc6b579fdc66af7697e5b2ea57887 to your computer and use it in GitHub Desktop.
Lamda function for taking manual RDS snapshot with email alerts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
const awsConf = { | |
accessKeyId: process.env.ACCESS_KEY, | |
secretAccessKey: process.env.SECRET_KEY, | |
region: process.env.REGION | |
} | |
const rdsConfig = { | |
apiVersion: '2014-10-31', | |
... awsConf | |
} | |
const snsConfig = { | |
apiVersion: '2010-03-31', | |
... awsConf | |
} | |
var rds = new AWS.RDS(rdsConfig); | |
var sns = new AWS.SNS(snsConfig); | |
function notifyUser(data, callback){ | |
const params = { | |
Message: data.message, | |
Subject: data.subject, | |
TopicArn: process.env.SNS_TOPIC_ARN | |
}; | |
sns.publish(params, callback); | |
} | |
exports.handler = (event, context, callback) => { | |
const currentDate = new Date(); | |
const params = { | |
DBInstanceIdentifier: 'my-db', /* required */ | |
DBSnapshotIdentifier: `my-db-${currentDate.toDateString().replace(/\s+/g, '-').toLowerCase()}-snapshot-manual-by-lamda`, /* DB Snapshot name */ | |
}; | |
rds.createDBSnapshot(params, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); // an error occurred | |
notifyUser({ | |
subject: "[AWS] RDS Manual Snapshot Failed", | |
message: ` | |
DB Instance : ${params.DBInstanceIdentifier} | |
Region : ${awsConf.region} | |
Error : ${err.stack} | |
` | |
}, callback) | |
} | |
else { | |
notifyUser({ | |
subject: "[AWS] RDS Manual Snapshot Started", | |
message: ` | |
DB Instance : ${params.DBInstanceIdentifier} | |
Region : ${awsConf.region} | |
Latest Snapshot : ${params.DBSnapshotIdentifier} | |
` | |
}, callback) | |
} // successful response | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment