Pull Request Notification on Slack using AWS Lambda
Tags: git, serverless, aws
Published: 2020-08-09 16:12:40

---
title: Pull Request Notification on Slack using AWS Lambda
published: true
description: Pull request notification on slack channel using aws lambda on backend
tags: #git, #serverless, #aws
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/11nkhto41rtb1tr9g4ow.png
---

Get notified on new pull request in your teams slack channel. This can be very useful if there are multiple repositories across a wide team. 

This post details on how to get github pull request notifications on slack channel

> Requirements:
  AWS account, Slack Channel, Github

### Step 1: Create a Slack Channel webhook

We need a slack webhook to post our events. Follow the below document and note down the webhook url
https://api.slack.com/tutorials/slack-apps-hello-world

Test the webhook using the command in that document to validate.

### Step 2: AWS Lambda backend

We will be using lambda to process the pull request event and send notification to slack.

have posted the starter code in the below github repository
clone / download the repo [github.com/uptownaravi/pullRequestSlack](https://github.com/uptownaravi/pullRequestSlack)

Then install the requirements.txt
```python
pip install --target="give-the-current-directory-path" -r requirements.txt
```

after that, this should be the folder structure. Then Zip these files.

![zip](https://dev-to-uploads.s3.amazonaws.com/i/kxk52oncm6j1c4w6j89g.JPG)

Then navigate to lambda console in aws.

create function --> author from scratch --> give it a name and runtime as Python 3.7

![create function](https://dev-to-uploads.s3.amazonaws.com/i/34vovr39c3wl9psvgqlu.JPG)

once created select actions --> upload a .zip file --> select the zip package we created earlier

![zip upload](https://dev-to-uploads.s3.amazonaws.com/i/gb3khvw4jpr8rjpw03ak.JPG)

lambda should be updated as below

![updated function](https://dev-to-uploads.s3.amazonaws.com/i/ujj4xhmi32ljcfnvpwqn.JPG)

scroll down to the environment variables section
add key as slackNotification and value as the slack webhook URL we created in step 1.

![env](https://dev-to-uploads.s3.amazonaws.com/i/41y2rmojsl0iphun96e6.JPG)

make sure that key is slackNotification as this will be used by lambda code.


Then navigate to API gateway and create API
select the HTTP method and click on build

![apig](https://dev-to-uploads.s3.amazonaws.com/i/ktluzdgku91kfhjio3ol.JPG)

choose add integration and select the lambda we created earlier

![integration](https://dev-to-uploads.s3.amazonaws.com/i/0mocffwp9e54ohj72lle.JPG)

then move on to next, select method as POST and resource path as /pull.

![route](https://dev-to-uploads.s3.amazonaws.com/i/sq8w4i4pq9mocad8ollx.JPG)

next give stage name as dev and let auto deploy be enabled.

![stage](https://dev-to-uploads.s3.amazonaws.com/i/zenwgn3cr8cf16lj1ofb.JPG)

once created, note down the api url

![apig created](https://dev-to-uploads.s3.amazonaws.com/i/r0dxlsuhrcqgnyhhqux7.JPG)

select CORS in the left section and configure it to have POST and GET in the Access-Control-Allow-Methods section

![cors](https://dev-to-uploads.s3.amazonaws.com/i/q2i0xxbue5hvaktrqliz.JPG)


### Step 3: Adding the API Gateway url to Github

We need github to send the pull request events to aws lambda through api gateway.

Navigate to your repository in github for which you need to get pull request notifications.

Go to settings in that repository

add the url in the payload url which we got from api gateway as below example 
https://sample.execute-api.ap-south-1.amazonaws.com/dev/pull

note: use the api gateway url from step 2 and make sure to add /pull to the end of that url. The above one is an example.

![github webhook](https://dev-to-uploads.s3.amazonaws.com/i/d328no4s856gv68d1i1c.JPG)

We need to keep the events to pull request to trigger the webhook.
choose let me select individual events and scroll down

and select pull request and un-check the Pushes --> Add webhook

![pullRequests](https://dev-to-uploads.s3.amazonaws.com/i/f895o1xunrl9a2l1y5rn.JPG)

That's it once you create a pull request in that repository, there will be a notification on the slack channel

![botChat](https://dev-to-uploads.s3.amazonaws.com/i/kbd3uv6a40b0xencbyct.JPG)

if required modify the lambda code to handle different type of pull request events.


> This is my first post in dev.to, looking forward to your comments.