Pull Request notification on Slack using AWS Lambda
Tags: git, aws, serverless, slack
Published: 2023-02-04 06:43:21

---
title: Pull Request notification on Slack using AWS Lambda
published: true
description: Get notified on the pull request in your repositories using AWS Lambda and API gateway 
tags: #git,#aws,#serverless,#slack
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/hde6txuf62p4zk4wz23z.png
---

Pull request management can become hectic while working across multiple repositories. Asking for approvals individually is also a long process. How about getting notified through a common channel to save time for reviewers and developers.

This post details on how to create a notification system on slack for github pull requests.


> Requirements
> aws account, github repository, slack channel ( with permission to create a slack app and webhook )

### Step 1 : Creating a slack app and webhook in a channel  <a name="step-1"></a>

* Slack has a good document on this, please follow that.
https://api.slack.com/tutorials/slack-apps-hello-world
once that is done we would get a webhook from slack which would be similar to 
https://hooks.slack.com/services/aldnfaksndksakd/aljdfkajndkjasn/adfasdfakjdfnaksdfkajldakdnkasndlakjd

you can test this with the curl command given in the same document. Note that down webhook url which will be used later.

### Step 2 : Creating Lambda and API gateway for processing the pull request event  <a name="step-2"></a>

we need to package the code required for notifications
clone or download the repo [github.com/uptownaravi/pullRequestSlack](https://github.com/uptownaravi/pullRequestSlack)

it will have lambda_function.py ( the logic ) and the requirements.txt ( dependency package )

we need to install that dependency and zip the files.
navigate to the cloned repository 

```python
pip install --target 'give the path of your current directory' -r requirements.txt
```
once that is done, we can see the below folder structure 

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

then select all the files there except the readme.md and requirements.txt and zip them ( in windows select send to compressed (Zipped) folder )

give the zip a name and 

* log into the aws console and navigate to lambda 
* click on create function
  * select author from scratch ( it should be the default )
  * give the function a name
  * select python 3.8 

![Create Function](https://dev-to-uploads.s3.amazonaws.com/i/idy8e86sndamgl157fnb.JPG)

Then click on actions --> upload .zip file

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

after upload it should load the code as seen below

![lambda Code](https://dev-to-uploads.s3.amazonaws.com/i/ei9ydxr9kxpk11ppzkn8.JPG)

We need to add slack token which we got earlier as a 
environment variables in the lambda function

scroll down in the lambda screen and click on manage environment variable --> add environment variable
fill in the details as below screen shot, use the webhook url which you got from slack. Make sure the key is slackNotification
( this is used in the lambda code to get that value )

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

> Note: We can encrypt the value in transit using KMS keys.
        but wanted to keep this blog simple
        use this link if required https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-encryption

#### API gateway creating and attaching this lambda

navigate to the api gateway in aws console and click on create api
then select http api --> click on build

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

then fill in the details as shown in the below image.
we need to use lambda as integration and select the lambda name we created in the last step ( make sure you are using the correct region )

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

click next and we will add the routes as shown below
using the post method here, so that github can post to this api

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

moving to next, add a stage name 

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

next review and click on create

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

already auto deployment is enabled for this api.

so go to stages in the deploy section and copy the invoke url 
this will be used in the github repository as post webhook

![deploy](https://dev-to-uploads.s3.amazonaws.com/i/77lypk4t2s9wchs9eof4.JPG)


### Step 3 : Adding the api gateway url to github

navigated to your github repistory --> click on settings --> then webhooks

add your api gateway in the below format **URL/pull** in the payload URL section
example: https://sample.execute-api.ap-south-1.amazonaws.com/dev/pull

( /pull is the route in api gateway, make sure the url will end with dev/pull so that that is used while calling the POST method )

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

select the **Let me select individual events**
and scroll down to select the Pull requests option and uncheck the Pushes 

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

then click on add webhook

that's it, create a pull request in that repository and it should create a notification on slack channel

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

you can modify the lambda code to handle various events in the pull requests.