/aws-codebuild-badges

Create CodeBuild badges using AWS Lambda and CloudWatch

Primary LanguageJavaScriptMIT LicenseMIT

aws-codebuild-badges

Build Status Dependencies Dev Dependencies

Introduction

AWS CodeBuild already has badges for builds however this sample code is helpful to learn the basics of AWS Lambda, CodeBuild & CloudWatch. Adapted from jSherz's code

Setup AWS

  1. Create S3 bucket
    • Make sure bucket has public access
    • e.g. bucket name: my-codebuild-badges
  2. Create Lambda function
    • Pick Author from scratch
    • Name update-build-badges
    • Runtime Node.js 8.10
    • Role Create a custom role
    • On new page, View Policy Document > Edit
    {
         "Version": "2012-10-17",
         "Statement": [
             {
                 "Effect": "Allow",
                 "Action": [
                     "logs:CreateLogGroup",
                     "logs:CreateLogStream",
                     "logs:PutLogEvents"
                 ],
                 "Resource": "arn:aws:logs:*:*:*"
             },
             {
                 "Sid": "AllowBadges",
                 "Effect": "Allow",
                 "Action": [
                     "s3:PutObject",
                     "s3:PutObjectAcl"
                 ],
                 "Resource": [
                     "arn:aws:s3:::my-codebuild-badges/*"
                 ]
             }
         ]
     }
    • Go back to previous page after saving your custom role and click Create Function
    • If you would like to modify the code, check development section below otherwise download the latest version here
    • Upload code to your function and set environment variable BUCKET=my-codebuild-badges
  3. Create CloudWatch event/trigger
    • Events > Rules > Create Rule
    • Service Name CodeBuild
    • Event Type CodeBuild Build State Change
    • Add target > Lambda function and select your lambda function
  4. Trigger your build and you will see a new svg file under s3://my-codebuild-badges with your CodeBuild project name.

Development

Modify the code as you wish and create a zip bundle:

$ npm install
// modify the code
$ npm run zip

index.zip will be generated in project's root. This file can be uploaded to AWS Lambda.