Description
When a GitHub pull request is made, it will trigger an alert on Slack. A magic night project provided by AWS User Group Hosted by MindTouch.
Tools used in this project
- Slack: smylee.slack.com
- GitHub: github.com/smyleeface/shiny-palm-tree
- Amazon API Gateway & Lambda
Slack Setup
-
Setup a Slack team if you don’t have one to use.
-
Create a new channel (or skip this step to use an existing channel that the messages will appear.)
-
From settings choose “Add an app or integration”.
-
In the textbox, type and choose “Incoming Webhooks”
-
If this is the first Webhook created, click on “install”, otherwise “configure”.
-
Configure a new Webhook into the desired channel.
-
Complete and save the form. Note the Webhook URL for use later.
AWS Lambda Setup
-
Make sure you note the region you are using.
-
Create a new Lambda Function; skip selecting a blueprint.
-
Configure the Lambda function:
-
Name the function.
-
Choose “Python 2.7” as the Runtime.
-
Download and copy/paste the code into the textarea.
-
In the code, replace
to the Webhook I mentioned in the Slack setup.
-
(optional) Choose a different emoji from the emjoi cheet sheet or change the username message in the code.
-
Use the default Handler (lambda_function.lambda_handler).
-
Create a new role with *Basic Execution Role.
- This will open a new window/tab; use the default IAM Role and Create a new Role Policy.
- This will open a new window/tab; use the default IAM Role and Create a new Role Policy.
-
Leave the rest of the settings default. You can choose your own VPC, but I’m choosing “No VPC” in this configuration.
-
Review and Create.
-
Test the function. On first test, Lambda will prompt for inputs. You can copy/paste the (VERY LONG) sample payload for the Pull Request Event from: https://developer.github.com/v3/activity/events/types/#pullrequestevent
-
Save and test! If you have your Slack open, you should’ve received a message in the channel where you setup the incoming Webhook. If you used the sample payload data, it will look something like this:
-
Now your Lambda function is ready to go, let’s connect to an API Gateway!
AWS API Gateway Setup
-
Setup a New API Gateway or use an existing Gateway if you have one setup.
-
Create a new Resource. Enter a name and use the default for the resource path.
-
Click on the new Resource and create a new Method > Post
-
Choose Lambda Function > us-west-2 > smylee_github_slack_alert, save. After saving, a permissions prompt will popup choose OK.
-
Click on the method function just created and four boxes will appear on the right side, click the Test Icon.
-
In the Request Body textarea, paste the same payload used for the pull request event. (https://developer.github.com/v3/activity/events/types/#pullrequestevent)
-
Click the Test button and if you have your Slack open, you should’ve recived a message in the channel you setup the incoming Webhook!
-
Now deploy the API from Actions > Deploy API.
-
In Deployment Stage, Choose [New Stage], in the stage name enter “prod” (this will be part of the API url.)
-
Once the API is created, within Stages from the left side, drill down to the resource and click the method.
-
This will display an “Invoke URL”, copy this URL and we’ll use it in the GitHub Setup section.
GitHub Setup
-
Create a new repository, add some files.
-
In the settings for the repository, go to Webhooks & Services > Add Webhook.
-
In the Payload URL, enter the URL from the API Gateway we just deployed.
Under “Which events would you like to trigger this webhook?”, choose: Let me select individual events. > Pull request
-
Save webhook.
Try it out!
-
In your repo, create a new file, name the file, add some text, and create a new branch, and propose new file to save.
-
Click the Create a pull request button.
-
If you have your Slack open, you should’ve received a message in the channel!
-
Try closing, opening, and merging the pull request from GitHub and you should receive messages for each action (merge will report closed).
Footnotes
- Now anytime a pull request is made on this repo it will alert you on Slack.
- If you want to customize the message, make the change in the slack_message inside the Lambda function.
- Troubleshooting note: Check your CloudWatch Logs /aws/lambda/< lambda function name > for messages that were logged.
AWS Costs for creating this project
- AWS provides a lot of services for free for the first 12 months of sign up and beyond. More information can be found on AWS pricing pages.
- Cost should be less than $0.10 to create this project.
- Lambda pricing cost - Doesn’t start to charge until you’ve made 1 million requests and gone over 400,000 GB-Seconds (each billed separately.)
- API Gateway pricing cost - Different for some regions, but all regions are billed per million calls received, plus the cost of data transfer out, in gigabytes. Plus you only pay for what you use.