Skip to content

Instantly share code, notes, and snippets.

@pratheekhegde
Created August 12, 2018 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pratheekhegde/9e7cc6b579fdc66af7697e5b2ea57887 to your computer and use it in GitHub Desktop.
Save pratheekhegde/9e7cc6b579fdc66af7697e5b2ea57887 to your computer and use it in GitHub Desktop.
Lamda function for taking manual RDS snapshot with email alerts.
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