AWS CodeDeploy success or failure email notification

0 votes
I have a deployment in AWS's CodeDeploy. I would like to receive an email notifying me about the success or the failure. Is this possible using the Lambda function?

If I could get some code or references it would be really helpful.

Thanks in advance!
Oct 15, 2019 in AWS by Hannah
• 18,520 points
1,304 views

1 answer to this question.

0 votes

Hi @Hannah, I found this code on Github and found it very useful. It finds out the status of the deployment on CodeCeploy and sends the notification to the slack via a lambda function: https://gist.github.com/teeli/29a0e79397fa5560e1819b80025981af

var https = require('https');
var util = require('util');

exports.handler = function (event, context) {
    console.log(JSON.stringify(event, null, 2));
    console.log('From SNS:', event.Records[0].Sns.Message);

    var severity = "good";
    var message = event.Records[0].Sns.Message;
    var messageJSON = JSON.parse(message);
    var subject = event.Records[0].Sns.Subject;

    var postData = {
        channel: "#channel",
        username: "CodeDeploy",
        icon_emoji: ":codedeploy:"
    };

    if (messageJSON.status == "FAILED") {
        severity = "danger"
    }
    if (messageJSON.status == "STOPPED") {
        severity = "warning"
    }

    var fields = [];
    for (var key in messageJSON) {
        if (key == 'deploymentOverview') {
            var value = [];
            var deploymentOverview = JSON.parse(messageJSON[key]);
            for (var status in deploymentOverview) {
                value.push(status + ': ' + deploymentOverview[status]);
            }
            fields.push({
                "title": key
                    .replace(/([A-Z])/g, ' $1')
                    .replace(/^./, function (str) {
                        return str.toUpperCase();
                    }),
                "value": value.join(', '),
            });
        } else {
            fields.push({
                "title": key
                    .replace(/([A-Z])/g, ' $1')
                    .replace(/^./, function (str) {
                        return str.toUpperCase();
                    }),
                "value": messageJSON[key],
                "short": true
            });
        }
    }

    postData.attachments = [
        {
            "color": severity,
            "fallback": message,
            "title": subject,
            "title_link": "https://console.aws.amazon.com/codedeploy/home?region=" + messageJSON.region + "#/deployments/" + messageJSON.deploymentId,
            "fields": fields
        }
    ];

    var options = {
        method: 'POST',
        hostname: 'hooks.slack.com',
        port: 443,
        path: '/services/yourservicehookhere'
    };

    var req = https.request(options, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            context.done(null);
        });
    });

    req.on('error', function (e) {
        console.log('problem with request: ' + e.message);
    });

    req.write(util.format("%j", postData));
    req.end();
};

answered Oct 15, 2019 by Jack

Related Questions In AWS

0 votes
1 answer

Email notification using SNS topic and Lambda function in AWS?

Hi@akhtar, You can set notification system for your ...READ MORE

answered Apr 2, 2020 in AWS by MD
• 95,460 points
4,652 views
0 votes
1 answer
0 votes
1 answer

if instance is a part of some AutoScaling Group in AWS or not ?

You can run this command below on ...READ MORE

answered Jun 2, 2018 in AWS by Cloud gunner
• 4,670 points
1,789 views
0 votes
1 answer
+3 votes
3 answers

Terraform AWS Cognito App Client

This feature is not currently supported by ...READ MORE

answered Aug 28, 2018 in AWS by eatcodesleeprepeat
• 4,710 points
3,621 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP