AWS CodePipeline and Slack Solutions

by DonOfDen

Posted on 25 Apr 2020

Tags: AWS Amazon CodePipeline Slack Integration Automation AWS Lambda SNS Topic CloudWatch

AWS CodePipeline and Slack Solutions

Do you want to get a notification about the deployment progress in your slack channel?

This blog is all about integrating AWS CodePipeline failures to a slack channel. In case of any failures in the pipeline, AWS Cloudwatch Event is triggerred which sends a notification on a slack channel using SNS & AWS Lambdas.

To do that we are going to use/integrate the follwing services:


AWS CodePipeline

As you all know, AWS CodePipeline is an Amazon Web Services product that automates the software deployment process.

Our goal is to notify us when a CodePipeline fails.

Following is my Pipeline list screen, what we are intrested in is the Name of the Pipeline.


SNS Topic

Amazon SNS allows applications to send time-critical messages to multiple subscribers through a “push” mechanism, eliminating the need to periodically check or “poll” for updates.

Step Details
1 Go to AWS Console -> Services -> SNS -> Create topic
2 Enter topic name as pipelines-failure-event
3 Select the above created topic
4 Click Create Subscription & select Protocol as AWS Lambda.
5 Select above created slack-alerts Lambda function as Endpoint & Create subscription.

AWS Lambda

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. You can use AWS Lambda to extend other AWS services with custom logic, or create your own back-end services that operate at AWS scale, performance, and security.

Step Details
1 Go to AWS Console -> Services -> Lambda -> Create Function
2 Provide function name as slack-alerts & select runtime as Python 2.7 or Python 3.6.
3 Choose Create Custom Role which will open up a new page for role creation.
4 Give role name as slack_alerts_lambda_role & Click on Allow.
5 Add code from to Function Code section
6 Add environment variables SLACK_WEBHOOK_URL:
7 Save this Lambda.


import json
import logging
import os
from urllib2 import Request, urlopen, URLError, HTTPError

# Read environment variables
logger = logging.getLogger()

def lambda_handler(event, context):"Event: " + str(event))
    # Read message posted on SNS Topic
    message = json.loads(event['Records'][0]['Sns']['Message'])"Message: " + str(message))
    # Construct a slack message
    slack_message = {
        'text': "%s" % (message)

    # Post message on SLACK_WEBHOOK_URL
    req = Request(SLACK_WEBHOOK_URL, json.dumps(slack_message))
        response = urlopen(req)"Message posted to %s", slack_message['channel'])
    except HTTPError as e:
        logger.error("Request failed: %d %s", e.code, e.reason)
    except URLError as e:
        logger.error("Server connection failed: %s", e.reason)

Add environment variables



  • Create Slack app
Step Details
1 Go to https://<your-team-domain>
2 Search for Incoming Webhooks in Search App Directory searchbox, from the left side menu.
3 Click on Add Configuration
4 Select a desired channel where you want to post notifications/alerts e.g. aws-alerts
5 Slack provides Webhook URL
6 You can customize Name & Icons as per your need



Amazon CloudWatch is a monitoring and management service that provides data and actionable insights for AWS.

With the name of the Pipeline, we can write cloudwatch event to capture any failure.

Step Details
1 Go to AWS Console -> Services -> CloudWatch -> Events -> Rules -> Create Rule
2 Select Service Name as CodePipeline & Event Type as CodePipeline Pipeline Execution State Change.
3 Select radio button Specific state(s) & choose FAILED from the dropdown
4 Click on Edit & update the json


  • Event Source
  "source": [
  "detail-type": [
    "CodePipeline Pipeline Execution State Change"
  "detail": {
    "state": [
    "pipeline": [
Step Details
5 For Targets, choose Add target.
6 In the list of targets, choose SNS topic. For Topic, enter the above SNS Topic name you created pipelines-failure-event.
7 Expand Configure input, and select Input Transformer.
8 In the Input Path box, copy {"pipeline":"$.detail.pipeline"}. In the Input Template box, copy `

":rotating_light:The Pipeline *<pipeline>* has failed.:man_shrugging:"

Step Details
9 Click Configure Details.
10 Give name as your-code-pipeline-failures in our case build-failure-for-ec2-service & leave state as Enabled.
11 Click Create Rule.

Slack Alerts

We are all done. Whenever this pipeline fails our CloudWatch Events will capture it and pass it to SNS Topic,It will inturn invoke the lambda function and you will get a notification over slack channel.



Send Notification From AWS Code Pipeline to Slack for more details.

AWS CodePipeline & Slack integration for more details.

Future Goal

  • Convert the manual step to CDK Deploy, So we can reduce all the manual steps to add this awesome automation.

  • Drive the CDK Deployment from config.json another AWS CDK menthod.