nwestfall / lambda-github-runner

Github Action Runner inside of Lambda - A Completely Serverless Solution
MIT License
85 stars 9 forks source link

Lambda GitHub Runner

A completely serverless way to have simple self-hosted runners for GitHub Actions. Lightweight, fast and easy to deploy.

Why it was built

Once Lambda Containers came out, I wanted to see how far they could go. I know there is already a serverless way of doing Github Runners with AWS Fargate, but if you can do it all in Lambda why not?

I also wanted to develop a way to do the last "job" in my actions in my own environment. This way, I could assign the AWS permissions to the lambda function and not have to rotate keys in GitHub, just use IAM roles.

How it Works

Using GitHub webhooks, we listen for the check_run event and then start up a runner when it's needed. This is now possible through Lambda Containers. It uses a few different AWS services to make this possible.

Using the lifecycle of an action and the check_run webhook, we are able to dynamically add/remove self-hosted runners from GitHub as they are needed.

See working example - https://github.com/nwestfall/lambda-runner-test/actions

Deployment Overview

Deployment Diagram

Generate a GitHub Token

A GitHub token is required to setup the project. This token just needs repo access. It is used to generate GitHub Runner tokens used to add/remove the runners.

How to Deploy

This project includes a Terraform so you can essentially set this up in your environment with little to no effort. It uses the AWS credentials on your machine and pulls the lambda functions (.zip and docker image) from a public source, so you don't have to build locally (but you can if you want). You will need the following on your machine to get started

Once you have everything above installed, just clone this repo, navigate to the deploy folder and run

terraform init

then

terraform plan

There are a number of variables you can provide before building to customize it in your account (listed below). Once you feel confortable about what it is setting up, just run the final command

terraform apply

Once it is successfully deployed, it will output your webhook URL (generated by API Gateway). This is what you will bring to your GitHub repository to setup webhooks.

Setting up GitHub Webhooks

Once you have the project deployed in your AWS account, you just need to setup the webhook endpoint (provided at the end of the terraform process) in the GitHub repository you wish to have use the runners.

Go to the GitHub repository and select "Settings" -> "WebHooks". You will see an option to "Add webhook". Enter and change the following information.

Known Limitations

More to Come

I do have some future plans for this project, including supporting a hidden "configuration" file in the repoistory (like how GitHub does workflows in .github) to configure more aspects of the runner and maybe even use "AWS Fargate" over Lambda in certain cases due to a longer execution time or some other variable. Open to ideas!