DEV Community

Cover image for Pull Request notification on Slack using AWS Lambda
Harish Aravindan
Harish Aravindan

Posted on

Pull Request notification on Slack using AWS Lambda

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

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

we need to package the code required for notifications
clone or download the repo 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

pip install --target 'give the path of your current directory' -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

once that is done, we can see the below folder structure

folderZip

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

Then click on actions --> upload .zip file

function upload

after upload it should load the code as seen below

lambda Code

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

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

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

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

moving to next, add a stage name

stage

next review and click on create

apig created

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

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

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

pullRequests

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

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

Top comments (1)

Collapse
 
whattheportal profile image
WTP | WhatThePortal.com

Thanks for writing this out!

These days we've found using the built-in "GitHub scheduled reminders" setting to be quite handy in doing the Slack notifications for us (we recently posted a Pull Requests guide outlining it too).

Still nice to see alternative ways to accomplish it custom! Thanks for the post