AWS CodePipeline & Slack integration

Krishna Kuntala
3 min readNov 4, 2018

Slack is an integral part of every Delivery & DevOps team. Technical team do not want to waste their time looking at different UIs or consoles for any failures or alerts. Instead they want to receive the notifications on a single tool which alerts them in case of any failures, updates, risks, etc. Slack comes to the rescue by providing web hooks, where you can integrate multiple applications for alerting and monitoring.

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.

Step 1: Slack Webhook
1. Go to https://<your-team-domain>.slack.com/apps/manage
2. Search for Incoming Webhooks in Search App Directory searchbox
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 https://hooks.slack.com/services/...
6. You can customize Name & Icons as per your need

Step 2: Create AWS Lambda
1. Go to AWS Console -> Services -> Lambda -> Create Function
2. Provide function name as slack-alerts & select runtime as Python 2.7
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 lambda_handler.py to Function Code section
6. Add environment variables
SLACK_CHANNEL: aws-alerts
SLACK_USER: Pipelines
SLACK_WEBHOOK_URL: https://hooks.slack.com/services/...
7. Save this Lambda

Step 3: Create SNS Topic
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-alertsLambda function as Endpoint & Create subscription.

Step 4: Create CloudWatch Events Rule
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 as:

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.:rotating_light:"
9.Click Configure Details
10. Give name as your-code-pipeline-failures & leave state as Enabled
11. Click Create Rule

Step 5: Slack Alerts
Execute your CodePipeline to test failure scenario. You should receive a slack alert:

--

--