How github-aws-runners/terraform-aws-github-runner works
1. What this repo is
- A Terraform-based AWS control plane for self-hosted GitHub Actions runners.
- The runtime system is split between AWS infrastructure and a set of Node.js Lambda functions that react to GitHub events, AWS events, and schedules.
2. Top-level composition
- The root module is a wrapper that composes several reusable submodules: runners, webhook, ssm, runner-binaries-syncer, ami-housekeeper, and termination-watcher.
- It also creates shared queues and wires the outputs between these subsystems, so the main diagram should show the root as an orchestration layer rather than a single monolith.
3. Inbound GitHub event path
- The primary entry point is the webhook subsystem.
- A GitHub App sends webhook events into an AWS-hosted webhook Lambda/API path, which then filters and normalizes events such as workflow job and check-run activity.
- From there, events either go directly to SQS for scale-up processing or go through EventBridge if that mode is enabled.
4. Queue-based scaling boundary
- The architecture uses SQS as the decoupling boundary between GitHub event ingestion and runner provisioning.
- The root module manages a build queue and optional DLQ; the webhook publishes into this queue, and the scaling Lambdas consume from it.
- This keeps GitHub-facing ingestion separate from AWS capacity actions and makes retries/backpressure explicit.
5. Scale-up / scale-down control plane
- The runners module is the core provisioning subsystem.
- It contains the logic for scale-up, scale-down, runner registration, queue inspection, and termination decisions.
- The scale-up path creates EC2-based runners on demand, while scale-down removes idle capacity and can scale to zero when no jobs are active.
6. Runner instance layer
- Runner capacity is implemented as EC2 instances, typically Spot but optionally on-demand.
- Instances are launched from a configurable AMI, subnet set, instance types, security groups, IAM role/profile, tags, and user-data bootstrap scripts.
- The repo supports Linux and Windows runners, plus architecture variants like x64 and arm64.
7. Runtime bootstrap and lifecycle
- The modules/runners/templates and images/ directories show two lifecycle strategies:
- User-data bootstrap on fresh instances.
- Prebuilt AMIs created with Packer for faster or more controlled startup.
- The images/ tree contains OS-specific Packer templates and install/start scripts, so the diagram should treat AMI build as a separate build-time pipeline feeding the runner fleet.
8. SSM as configuration and secret boundary
- AWS SSM Parameter Store is used heavily for GitHub App credentials, webhook secrets, runner tokens, and JIT configuration.
- The ssm module and the aws-ssm-util library exist to manage parameter paths and cleanup responsibilities.
- This keeps sensitive bootstrap material out of the Lambda code paths and out of instance user-data where possible.
9. Supporting Lambdas around the core loop
- runner-binaries-syncer keeps GitHub runner binaries available in AWS, typically via S3-backed distribution.
- ami-housekeeper cleans up old AMIs and related artifacts.
- termination-watcher listens for EC2 spot termination signals and warning events so the system can react before or during interruption.
- job-retry and pool are optional/experimental extensions for retry handling and scheduled baseline capacity.
10. Eventing and scheduling
- There are two distinct kinds of AWS event sources in the design:
- SQS for workflow-driven demand.
- EventBridge schedules for periodic actions such as scale-down checks, pool resizing, SSM cleanup, and binary/AMI housekeeping.
- This makes the system both reactive to GitHub work and proactive about cleanup and cost control.
11. Security and access boundaries
- The modules define dedicated IAM policies for each Lambda and for the EC2 runner instances.
- There is support for permissions boundaries, security groups, KMS-encrypted logs/queues/parameters, and optional SSM access on runners.
- The repo also supports repository allow-lists and label checks so only intended GitHub workflows can consume runners.
12. Artifact and deployment pipeline
- Lambda code lives in lambdas/functions/* as TypeScript/Node.js projects with unit tests and local entrypoints.
- Terraform can deploy Lambdas from local zip files or S3 artifacts, so build outputs are external to the module code itself.
- GitHub Actions workflows cover Terraform validation, Lambda build/release, Packer image builds, docs generation, and security scanning.
13. Example-driven usage
- The examples/ directory contains deployable scenarios for default runners, ephemeral runners, prebuilt AMIs, external SSM-managed secrets, multi-runner setups, permissions boundaries, and termination watching.
- These examples are important because they show the intended composition patterns for the core modules and the different runtime topologies the repo supports.
14. How to draw the diagram
- Show GitHub as the external producer of webhook events.
- Show AWS Lambda + SQS/EventBridge + SSM + S3 + EC2 as the internal control plane and runner plane.
- Put the Terraform root module above the AWS components as the composition layer.
- Treat Packer AMI builds and Lambda artifact builds as deployment-time pipelines feeding the runtime system, not part of the live request path.
Main entry points: multi-runner/main.tf Multi runner · runners/main.tf Runners · webhook/main.tf Webhook · ssm.tf SSM
Components 22
Terraform composition
Root and reusable Terraform modules that wire the system together
- Root module · Composes the shared runner control plane and wires module outputs together
main.tf - Multi runner · Public composition module that assembles runners, webhook, queues, SSM, and watchers
modules/multi-runner/main.tf
Runtime control plane
Live AWS components handling events, scaling, secrets, and runner lifecycle
- Webhook · GitHub event ingress and dispatch wiring
modules/webhook/main.tf - Direct path · Normalizes events and publishes directly to SQS
modules/webhook/direct/webhook.tf - EventBridge path · Optional dispatcher that forwards webhook events through EventBridge
modules/webhook/eventbridge/webhook.tf - GitHub App · Produces repository webhooks and app credentials for the control plane
- Build queue · Decouples webhook demand from runner provisioning
modules/multi-runner/queues.tf - Runners · Core scaling and instance lifecycle subsystem
modules/runners/main.tf - Scale up · Inspects demand and launches EC2 runners on demand
modules/runners/scale-up.tf - Scale down · Finds idle capacity and reduces runner fleet size
modules/runners/scale-down.tf - Pool · Optional baseline capacity and periodic resizing logic
modules/runners/pool/main.tf - SSM · Parameter Store boundary for app secrets, tokens, and bootstrap config
modules/ssm/ssm.tf - Termination watcher · Watches EC2 interruption warnings and termination events
modules/termination-watcher/main.tf - AMI housekeeper · Schedules cleanup of old AMIs and related artifacts
modules/ami-housekeeper/main.tf - Runner syncer · Keeps GitHub runner binaries available in AWS
modules/runner-binaries-syncer/main.tf - EC2 runners · Spot or on-demand runner instances launched from AMIs or bootstrap user-data
- EventBridge · Optional event routing and scheduled triggers for housekeeping and resizing
- SQS · Buffer between GitHub ingestion and AWS capacity actions
- EC2 · Underlying instance platform for self-hosted runners
- S3 · Distribution target for runner binaries and optional Lambda artifacts
Build pipelines
Artifact and image build-time pipelines feeding the runtime
- AMI builds · Build-time image pipeline for Linux and Windows runner AMIs
images/README.md - Lambda builds · TypeScript/Node.js build and release pipeline for Lambda artifacts
lambdas/package.json
Connections 31
- Root module → Multi runner: composes · Root module wires the reusable multi-runner stack together
- Multi runner → Webhook: includes · Multi-runner stack provisions the webhook subsystem
- Multi runner → Runners: includes · Multi-runner stack provisions the core runner control plane
- Multi runner → SSM: includes · Multi-runner stack provisions shared SSM parameters
- Multi runner → Termination watcher: includes · Multi-runner stack provisions interruption handling
- Multi runner → AMI housekeeper: includes · Multi-runner stack provisions AMI cleanup
- Multi runner → Runner syncer: includes · Multi-runner stack provisions runner binary sync
- Multi runner → Build queue: creates · Stack defines the shared queue boundary
- GitHub App → Webhook: sends events · GitHub App posts workflow and check-run events to the webhook
- Webhook → Direct path: routes · Webhook module selects the direct ingestion path
- Webhook → EventBridge path: routes · Webhook module can enable EventBridge dispatch
- Direct path → SQS: publishes · Normalized events are enqueued for scaling
- EventBridge path → EventBridge: publishes · Webhook events are forwarded into EventBridge
- EventBridge → SQS: triggers · Scheduled or dispatched events ultimately feed queue-based scaling
- SQS → Scale up: consumes · Scale-up Lambda reacts to queued demand
- Runners → Scale up: contains · Scale-up logic lives inside the runner module
- Runners → Scale down: contains · Scale-down logic lives inside the runner module
- Runners → Pool: contains · Optional baseline capacity logic is part of the runner module
- Scale up → EC2 runners: launches · Scale-up provisions runner instances
- Scale down → EC2 runners: terminates · Scale-down removes idle runner instances
- EC2 runners → EC2: runs on · Runner capacity is implemented as EC2 instances
- SSM → Webhook: secrets · Webhook reads app credentials and secrets from Parameter Store
- SSM → Runners: config · Runner control plane reads tokens and configuration from Parameter Store
- SSM → EC2 runners: bootstrap · Instances may consume SSM parameters during startup and operation
- Termination watcher → EC2 runners: watches · Interruption warnings are tied to runner instances
- AMI housekeeper → AMI builds: cleans up · Housekeeper manages old AMIs produced by image builds
- AMI builds → EC2 runners: feeds · Prebuilt AMIs are consumed by the runner fleet
- Lambda builds → Webhook: builds · Lambda code is packaged and deployed as artifacts
- Lambda builds → Runners: builds · Control-plane Lambda code is packaged and deployed as artifacts
- Runner syncer → S3: publishes · Runner binaries are distributed via S3-backed artifacts
- Runners → S3: reads binaries · Runner startup and downloads depend on stored artifacts