babel-bot
uses AWS Lambda and AWS API Gateway to listen for and act on GitHub webhooks. When making changes to the bot, it is useful to setup a separate repository + Lambda function to try things in a test-like environment.
- Create an
AWS
Account. Note: A credit/debit card will be required - Create a new GitHub repository that you will use for testing purposes
- Visit the
Lambda
service section in the AWS Console - Click the
Create a Lambda Function
button, then chooseBlank Function
as the blueprint - Under
Configure Triggers
, clickNext
- Give your function a
Name
andDescription
, and set theRuntime
toNode.js 4.3
- Leave the
Lambda function code
with the default code (will be changed later) - Under
Lambda function handler and role
, choose,Create a new role from template(s)
- For
Role name
, use an identifier you will remember (example:BabelBotRole
) - For
Policy Templates
, chooseSimple Microservice Permissions
- Click
Next
at the bottom of the page - On the
Review
page, clickCreate Function
-
Visit the
API Gateway
service section in the AWS Console -
Click
Create API
-
Enter
BabelBotAPI
forAPI Name
, and enter anything forDescription
, then clickCreate API
-
On the page for your new API, click
Actions
>>Create Resource
-
For
Resource Name
, enterEvent
. LeaveResource Path
as the default (/event
). Then clickCreate Resource
-
On the page for the new resource, click
Actions
>>Create Method
-
Choose
POST
, then click the✓
button. You will be redirected to aSetup
page -
Set
Integration Type
toLambda Function
. -
For the
Lambda Region
field, you will need to set this to the region your function is in. You can find this by visiting your function in theLambda
console. -
For the
Lambda Function
field, enter the name of the function you created. Then clickSave
-
Verify you were redirected to a page with the title
/event - POST - Method Execution
. Then, click theIntegration Request
header -
Click to expand the
Body Mapping Templates
header -
Set the option for
When there are no templates defined (recommended)
-
Click
Add mapping template
, enterapplication/json
, then click the✓
button -
In the template field, copy/paste the following JSON, then click
Save
{ "signature": "$input.params('X-Hub-Signature')", "type": "$input.params('X-GitHub-Event')", "data" : $input.json('$') }
-
Under
Resources
, clickActions
>>Deploy API
-
Select
Deployment Stage
>>[New Stage]
-
For
Stage Name
, entertest
, then clickDeploy
. -
The success page should show a
Invoke URL
. Save this, as it will be used during theGitHub
setup
- Navigate to your test repository
- Visit
Settings
>>Webhooks
, and clickAdd Webhook
- In
Payload URL
, enter theInvoke URL
you copied from theAPI Gateway
setup step, and add/event
to the end of it - In
Content type
, selectapplication/json
- On the CLI, run
ruby -rsecurerandom -e 'puts SecureRandom.hex(20)'
. Then add the value to theSecret
field. Important: Save this value somewhere safe - Choose
Let me select individual events.
, and chooseIssues
andPull request
- Click
Add webhook
- Navigate to the
Tokens
page in yourGitHub
account settings - Create a new API Token, and give it the
Repo
permissions. Make sure to save this token!
- Navigate to https://circleci.com/account/api and click "Create New Token", give it a name, and click "Add API Token".
- Go back to the
Lambda
dashboard inAWS
, and click on your function to edit it - For
Code entry type
, chooseUpload a .ZIP file
- Run
yarn run package
in the root of this repository (make sure you've runyarn
first), then upload thefunction.zip
file in the root - Under
Environment variables
, add a variable forGITHUB_API_KEY
andGITHUB_SECRET
, using the values you setup in theGitHub
section of this document - Under
Environment variables
, add a variable forCIRCLECI_API_KEY
, using the values you setup in theCircleCI
section of this document - Click
Save
If everything was done correctly, your bot should now be live! To see the execution logs, you can click the Monitoring
tab on your function page in Lambda
, then click View logs in CloudWatch
.