An AWS Lambda function to receive GitHub webhooks from API gateway and relay them to an EC2 instance.
- Create a Security Group for the Lambda function
- Inbound: None
- Outbound: Only allow HTTPS/HTTP to the receiving instance
- Create a Lambda function:
- Runtime: Python 2.7
- Handler: hook.lambda_handler
- Role: Basic With VPC
- Memory: 128MB
- Timeout: 30sec
- VPC: The VPC of the receiving instance
- Subnets: At least 2 private subnets within the VPC
- Security Groups: The Security Group configured previously
- Install dependencies locally:
$ pip install -r requirements.txt -t lambdawebhook/lib/
- Create a ZIP archive of the
lambdawebhookdirectory:
$ cd lambdawebhook$ zip -r lambdawebhook.zip *
Upload the zipped code to the Lambda function created previously
Create an API in API gateway
Create a resource for
/githubCreate a POST method for
/github
- Integration type: Lambda Function
- Lambda Region: The region of the Lamba function created previously
- Lambda Function: The Lambda function created previously
- Integration Request -> Mapping Templates:
Content-Type:
application/jsonMapping Template (replace
secretandjenkins_urlas appropriate for your configuration):{ "x_github_delivery": "$util.escapeJavaScript($input.params().header.get('X-GitHub-Delivery'))", "x_github_event": "$util.escapeJavaScript($input.params().header.get('X-GitHub-Event'))", "x_hub_signature": "$util.escapeJavaScript($input.params().header.get('X-Hub-Signature'))", "secret": "some_secret", "jenkins_url": "https://jenkins/github-webhook/", "payload": "$util.base64Encode($input.body)" }
- Method Response:
- HTTP Status:
400
- Integration Response -> Add integration response:
- Lambda Error Regex:
400 Client Error: Bad Request - Method response status:
400 - Mapping Templates:
Content-Type:
application/jsonTemplate:
{ "message": $input.json('$.errorMessage') }
Deploy API
Configure the webhook and secret for the GitHub repository using the API URL provided in the previous step, and
secretset above.Test by pushing some code to the repository.
Linting (flake8) and testing (unittest) are executed using tox in the root directory of this repository:
$ pip install tox
$ tox