Using AWS Serverless + CodeBuild to provide Github webhooks services for auto-building mh-opsworks components. Currently this only includes prepackaging our custom chef cookbook.
You'll need Python 2|3, plus the awscli
package installed. Optionally, for local testing, you can use AWS's sam tool.
The following AWS services are used, so corresponding IAM permissions are necessary:
- Cloudformration
- S3
- API Gateway
- Lambda
- CodeBuild
There are two (optionally three) commands to be run, the end result of which will be a Cloudformation stack containing all the resources necessary to act as a webhook that can be plugged into Github.
The build process requires an s3 bucket for two purposes:
- storing local artifacts specified in the Cloudformation template (e.g., our Lambda function code).
- storing the resulting CodeBuild artifacts, i.e., the packaged cookbook archives
If not using an existing bucket, create one:
aws s3 mb s3://[artifact-bucket-name] --region [region]
aws cloudformation package \
--template-file template.yml
--output-template-file serverless-output.yml
--s3-bucket [artifact-bucket-name]
aws cloudformation deploy \
--template-feil serverless-output.yml
--stack-name mh-opsworks-builder
--capabilities CAPABILITY_NAMED_IAM
--parameter-orverrides BuildBucketName=[artifact-bucket-name]
Once the stack has completed buildout, check the resources list in the Cloudformation web console and click the link to the API Gateway instance. Find the API's "Prod" stage buildcookbook
method and copy the invoke URL. It should look something like https://foobarbaz.execute-api.us-east-1.amazonaws.com/Prod/buildcookbook. Copy the URL and head over to the Github project. In "Settings" -> "Webhooks" choose "Add Webhook". Paste the URL into the "Payload URL" field, set the content-type to "application/json". Choose "Let me select individual events" and select "Create" and "Push". Submit and you're done.
The cloudformation template takes as a paramter a HipChat notification URL to allow posting CodeBuild
build submissions and success status. If not provided, HipChat notifications will be disabled. The URL value
should be provided as a parameter override to the aws cloudformation deploy
command, like so...
--parameter-overrides BuildBucketName=[artifact-bucket-name] HipchatNotifyUrl=[url]
The notfication URL for a room can be obtained via the "Integrations" web console of your HipChat account.