How to Autoscale GitHub Runners on AWS EC2 With Terraform
The philips-labs Terraform module launches ephemeral EC2 runners in response to workflow_job webhooks and scales them down when idle.
The philips-labs/terraform-aws-github-runner module wires a GitHub App webhook to Lambda functions that launch ephemeral EC2 instances per queued job and terminate them after. It supports spot instances, pools, and scale-to-zero, so you get on-demand runners without managing servers.
Steps
- Create a GitHub App and configure the
workflow_jobwebhook. - Point the module at your VPC subnets and the App credentials.
- Enable
enable_ephemeral_runnersso each instance runs one job then terminates.
main.tf
Terminal
module "runners" {
source = "philips-labs/github-runner/aws"
aws_region = "us-east-1"
vpc_id = var.vpc_id
subnet_ids = var.private_subnets
github_app = {
key_base64 = var.github_app_key_base64
id = var.github_app_id
webhook_secret = var.webhook_secret
}
runner_extra_labels = ["ephemeral", "linux"]
enable_ephemeral_runners = true
instance_types = ["m5.large"]
}Gotchas
- Ephemeral runners require the
workflow_jobwebhook so the scale-up Lambda fires per job. - Give the module private subnets with a NAT so runners reach GitHub without public IPs.
Related guides
How to Start an EC2 Runner Per Job With ec2-github-runnerSpin up a fresh EC2 runner for a single job with the machulav/ec2-github-runner action, run your work, then t…
How to Run CI Runners on Spot Instances and Handle InterruptionRun self-hosted CI runners on spot or preemptible instances to cut cost, and handle the interruption notice b…