npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mapbox/aws-github-webhook

v1.0.0

Published

Create an API Gateway endpoint to receive Github webhooks

Downloads

10

Readme

aws-github-webhook

A simple helper to build a connection between Github webhooks and your AWS Lambda functions.

When to use it

Use this module when you wish to invoke a Lambda function in reaction to webhooks delivered by Github in response to push actions only. Over time, this library may grow to support other event types.

What it does

This library provides you with a set of CloudFormation resources to include in a CloudFormation template alongside the Lambda function you wish to have invoked. The provided resources define an API Gateway endpoint and functionality to authenticate webhook payloads coming from Github.

You provide a Lambda function that reacts to push events, and we provide the rest.

The data your Lambda function receives

It will be a shortened version of a Github push event. Here's an example:

{
  "ref": "refs/head/changes",
  "after": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
  "before": "9049f1265b7d61be4a8904a9a27120d2064dab3b",
  "deleted": false,
  "repository": {
    "name": "public-repo",
    "owner": {
      "name": "baxterthehacker"
    }
  },
  "pusher": {
    "name": "baxterthehacker"
  }
}

However that data will be "wrapped" a few levels deep. To access the data as a JavaScript object, you will want your Lambda function's code to parse the incoming event as follows:

module.exports.handler = (event, context, callback) => {
  const message = JSON.parse(event.Records[0].Sns.Message);
}

How to use this module

  1. Create a CloudFormation template (in JavaScript) that defines
  • a Lambda function that will react to Github webhook payloads, and perform whatever actions you wish to tak on each push, and
  • the IAM role that your Lambda function will need in order to accomplish its task.
  1. Use the JavaScript function exported by this module to create the rest of the resources required. Merge those resources with your own using cloudfriend.
  2. Create the CloudFormation stack by deploying your template using cfn-config.
  3. Your stack will output the URL for your webhook's endpoint, and a secret token. Provide these values to Github as a new webhook (see settings/hooks/new for your repository). Make sure to specify the Content type as application/json.

Example CloudFormation template

You should make a CloudFormation template file that looks similar to this:

const cf = require('@mapbox/cloudfriend');
const buildWebhook = require('@mapbox/aws-github-webhook');

const myTemplate = {
  Resources: {
    MyLambda: {
      Type: 'AWS::Lambda::Function',
      Properties: {
        Code: {
          S3Bucket: 'my-bucket',
          S3Key: 'my-code.zip'
        },
        FunctionName: 'MyGithubWebhook',
        Handler: 'index.handler',
        MemorySize: 256,
        Runtime: 'nodejs6.10',
        Timeout: 300,
        Role: cf.getAtt('LambdaRole', 'Arn')
      }
    },
    LambdaRole: {
      Type: 'AWS::IAM::Role',
      Properties: {
        AssumeRolePolicyDocument: {
          Statement: [
            {
              Effect: 'Allow',
              Principal: { Service: 'lambda.amazonaws.com' },
              Action: 'sts:AssumeRole'
            }
          ]
        },
        Policies: [
          {
            PolicyName: 'write-logs',
            PolicyDocument: {
              Statement: [
                {
                  Effect: 'Allow',
                  Action: 'logs:*',
                  Resource: 'arn:aws:logs:*'
                }
              ]
            }
          }
        ]
      }
    }
  }
};

const webhook = buildWebhook('MyLambda');

module.exports = cf.merge(myTemplate, webhook);

Using the Serverless Application Model

This can be especially useful if you have a simple lambda function that doesn't require a lot of additional AWS permissions in order to complete its work. Here's an example:

const cf = require('@mapbox/cloudfriend');
const buildWebhook = require('@mapbox/aws-github-webhook');

const myTemplate = {
  Resources: {
    MyLambda: {
      Type: 'AWS::Serverless::Function',
      Properties: {
        Handler: 'index.handler',
        Runtime: 'nodejs6.10',
        CodeUri: 's3://my-bucket/my-code.zip'
      }
    }
  }
};

const webhook = buildWebhook('MyLambda');

module.exports = cf.merge(myTemplate, webhook);

View a complete example template as JSON

If you're interested in getting a better understanding of the CloudFormation resources that are built in order to support this workflow, try taking a closer look at the full JSON behind an example template.

$ cd ~/aws-github-webhook
$ npm run example