Using serverless AWS technology (AWS Lamdba & SNS) with a GitHub Webhook to process GitHub events

Author: Martijn Scholten (https://twitter.com/mscholten1988)

In our previous article, we talked about creating feature branches on AWS for Single Page Applications. This works perfectly for us if we want to show new features to customers or demo them internally. The downside is after a while we ended up with a bunch of unused buckets and route 53 records after the features went trough the review and test process. In this article I will show you the removal of these buckets and route 53 recordsets.

Overview

First create a SNS Topic

This topic will be used by Github to publish events and the SNS topic will invoke your AWS lambda.

  1. Go to the Amazon SNS console.
  2. Click “Create topic”.
  3. Fill in the name and display name, then click “Create topic”.
  4. Copy the topic ARN for later use.
  5. If you want to enable Delivery status feature or else skip to the next chapter: Go to the topic in SNS->Topics.
  6. Click “Delivery Status”.
  7. Click and enable “Lambda”.
  8. Click “Create IAM Roles” and create the IAM roles.

Create an IAM User with a custom policy for publishing to SNS

  1. Go to the Amazon IAM console in the AWS Management Console.
  2. Click “Users”.
  3. Click “Create New Users”.
  4. Fill in a user name and make sure “Generate an access key for each user” is enabled.
  5. Save the access key for later use.
  6. Attach the policy below as a custom policy and make sure you fill in the ARN of the SNS Topic.

https://gist.github.com/scholtenmartijn/7807f04d932998c854f8626758c9b438

Create an IAM role for the Lambda execution

  1. Go to the Amazon IAM console in the AWS Management Console.
  2. Click “Roles”.
  3. Click “Create New Role”.
  4. Fill in a name and click “Next Step”.
  5. Select AWS Lambda.
  6. Don’t select a policy.
  7. Click “Create Role”.
  8. Attach the following inline policy to the newly created role:

https://gist.github.com/scholtenmartijn/c4449e3d79f0263e0a124c677f862ae1

Set up GitHub Webhook

  1. Navigate to your GitHub repo.
  2. Click on “Settings” in the sidebar.
  3. Click on “Webhooks & Services”.
  4. Click the “Add service” dropdown, then click “AmazonSNS”.
  5. Fill out the form (supplying the IAM user credentials you created in Step 2), then click “Add service”. Make sure you fill in the ARN in SNS Topic.

Create a Lambda function

In this example NodeJS is being used for deleting several assets (S3 and Route53) on AWS after a branch on Github is deleted.

  1. Open the AWS Lambda console.
  2. Click on “Create a Lambda function”.
  3. Choose the “SNS Message” code template and “Basic execution role”.
  4. Click “Create Lambda function”.
  5. On the Lambda function list page, click the “Actions” dropdown then pick “Add event source”.
  6. Select “SNS” as the event source type.
  7. Choose the SNS topic you created in Step 1, then click “Submit”. (Lambda will fill in the ARN for you.)

https://gist.github.com/scholtenmartijn/448c37db6a63dbb12d269a4d748fdf5a