# github-aws-runners/terraform-aws-github-runner architecture

> Architecture diagram of the GitHub repository github-aws-runners/terraform-aws-github-runner, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/github-aws-runners/terraform-aws-github-runner
- Repository: https://github.com/github-aws-runners/terraform-aws-github-runner

## Overview

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: [`modules/multi-runner/main.tf`](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/multi-runner/main.tf) (Multi runner), [`modules/runners/main.tf`](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/main.tf) (Runners), [`modules/webhook/main.tf`](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/webhook/main.tf) (Webhook), [`modules/ssm/ssm.tf`](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/ssm/ssm.tf) (SSM)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_root["Terraform composition"]
  node_root["Root module<br/>terraform root<br/>[main.tf]"]
  node_multi_runner["Multi runner<br/>terraform stack<br/>[main.tf]"]
end

subgraph group_grp_runtime["Runtime control plane"]
  node_webhook["Webhook<br/>terraform module<br/>[main.tf]"]
  node_webhook_direct["Direct path<br/>webhook route<br/>[webhook.tf]"]
  node_webhook_eventbridge["EventBridge path<br/>webhook route<br/>[webhook.tf]"]
  node_github_app(("GitHub App<br/>external source"))
  node_build_queue["Build queue<br/>sqs queue<br/>[queues.tf]"]
  node_runners["Runners<br/>terraform module<br/>[main.tf]"]
  node_scale_up["Scale up<br/>lambda workflow<br/>[scale-up.tf]"]
  node_scale_down["Scale down<br/>lambda workflow<br/>[scale-down.tf]"]
  node_pool["Pool<br/>capacity policy<br/>[main.tf]"]
  node_ssm[("SSM<br/>terraform module<br/>[ssm.tf]")]
  node_termination_watcher["Termination watcher<br/>terraform module<br/>[main.tf]"]
  node_ami_housekeeper["AMI housekeeper<br/>terraform module<br/>[main.tf]"]
  node_runner_syncer["Runner syncer<br/>terraform module<br/>[main.tf]"]
  node_ec2_runners{{"EC2 runners<br/>runner fleet"}}
  node_eventbridge(("EventBridge<br/>aws event bus"))
  node_sqs["SQS<br/>aws queue"]
  node_ec2{{"EC2<br/>aws compute"}}
  node_s3[("S3<br/>artifact store")]
end

subgraph group_grp_build["Build pipelines"]
  node_packer_images["AMI builds<br/>packer pipeline<br/>[README.md]"]
  node_lambda_builds["Lambda builds<br/>artifact pipeline<br/>[package.json]"]
end

node_root -->|"composes"| node_multi_runner
node_multi_runner -->|"includes"| node_webhook
node_multi_runner -->|"includes"| node_runners
node_multi_runner -->|"includes"| node_ssm
node_multi_runner -->|"includes"| node_termination_watcher
node_multi_runner -->|"includes"| node_ami_housekeeper
node_multi_runner -->|"includes"| node_runner_syncer
node_multi_runner -->|"creates"| node_build_queue
node_github_app -->|"sends events"| node_webhook
node_webhook -->|"routes"| node_webhook_direct
node_webhook -->|"routes"| node_webhook_eventbridge
node_webhook_direct -->|"publishes"| node_sqs
node_webhook_eventbridge -->|"publishes"| node_eventbridge
node_eventbridge -.->|"triggers"| node_sqs
node_sqs -->|"consumes"| node_scale_up
node_runners -->|"contains"| node_scale_up
node_runners -->|"contains"| node_scale_down
node_runners -->|"contains"| node_pool
node_scale_up -->|"launches"| node_ec2_runners
node_scale_down -->|"terminates"| node_ec2_runners
node_ec2_runners -->|"runs on"| node_ec2
node_ssm -->|"secrets"| node_webhook
node_ssm -->|"config"| node_runners
node_ssm -.->|"bootstrap"| node_ec2_runners
node_termination_watcher -->|"watches"| node_ec2_runners
node_ami_housekeeper -.->|"cleans up"| node_packer_images
node_packer_images -->|"feeds"| node_ec2_runners
node_lambda_builds -.->|"builds"| node_webhook
node_lambda_builds -.->|"builds"| node_runners
node_runner_syncer -->|"publishes"| node_s3
node_runners -.->|"reads binaries"| node_s3

click node_root "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/main.tf"
click node_multi_runner "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/multi-runner/main.tf"
click node_webhook "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/webhook/main.tf"
click node_webhook_direct "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/webhook/direct/webhook.tf"
click node_webhook_eventbridge "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/webhook/eventbridge/webhook.tf"
click node_build_queue "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/multi-runner/queues.tf"
click node_runners "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/main.tf"
click node_scale_up "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/scale-up.tf"
click node_scale_down "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/scale-down.tf"
click node_pool "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/pool/main.tf"
click node_ssm "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/ssm/ssm.tf"
click node_termination_watcher "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/termination-watcher/main.tf"
click node_ami_housekeeper "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/ami-housekeeper/main.tf"
click node_runner_syncer "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runner-binaries-syncer/main.tf"
click node_packer_images "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/images/README.md"
click node_lambda_builds "https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/lambdas/package.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_root,node_multi_runner toneBlue
class node_webhook,node_webhook_direct,node_webhook_eventbridge,node_github_app,node_build_queue,node_runners,node_scale_up,node_scale_down,node_pool,node_ssm,node_termination_watcher,node_ami_housekeeper,node_runner_syncer,node_ec2_runners,node_eventbridge,node_sqs,node_ec2,node_s3 toneAmber
class node_packer_images,node_lambda_builds toneMint
```

## Components

### Terraform composition

Root and reusable Terraform modules that wire the system together

- [Root module](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/main.tf): terraform root · Composes the shared runner control plane and wires module outputs together · `main.tf`
- [Multi runner](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/multi-runner/main.tf): terraform stack · 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](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/webhook/main.tf): terraform module · GitHub event ingress and dispatch wiring · `modules/webhook/main.tf`
- [Direct path](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/webhook/direct/webhook.tf): webhook route · Normalizes events and publishes directly to SQS · `modules/webhook/direct/webhook.tf`
- [EventBridge path](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/webhook/eventbridge/webhook.tf): webhook route · Optional dispatcher that forwards webhook events through EventBridge · `modules/webhook/eventbridge/webhook.tf`
- **GitHub App**: external source · Produces repository webhooks and app credentials for the control plane
- [Build queue](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/multi-runner/queues.tf): sqs queue · Decouples webhook demand from runner provisioning · `modules/multi-runner/queues.tf`
- [Runners](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/main.tf): terraform module · Core scaling and instance lifecycle subsystem · `modules/runners/main.tf`
- [Scale up](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/scale-up.tf): lambda workflow · Inspects demand and launches EC2 runners on demand · `modules/runners/scale-up.tf`
- [Scale down](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/scale-down.tf): lambda workflow · Finds idle capacity and reduces runner fleet size · `modules/runners/scale-down.tf`
- [Pool](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runners/pool/main.tf): capacity policy · Optional baseline capacity and periodic resizing logic · `modules/runners/pool/main.tf`
- [SSM](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/ssm/ssm.tf): terraform module · Parameter Store boundary for app secrets, tokens, and bootstrap config · `modules/ssm/ssm.tf`
- [Termination watcher](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/termination-watcher/main.tf): terraform module · Watches EC2 interruption warnings and termination events · `modules/termination-watcher/main.tf`
- [AMI housekeeper](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/ami-housekeeper/main.tf): terraform module · Schedules cleanup of old AMIs and related artifacts · `modules/ami-housekeeper/main.tf`
- [Runner syncer](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/modules/runner-binaries-syncer/main.tf): terraform module · Keeps GitHub runner binaries available in AWS · `modules/runner-binaries-syncer/main.tf`
- **EC2 runners**: runner fleet · Spot or on-demand runner instances launched from AMIs or bootstrap user-data
- **EventBridge**: aws event bus · Optional event routing and scheduled triggers for housekeeping and resizing
- **SQS**: aws queue · Buffer between GitHub ingestion and AWS capacity actions
- **EC2**: aws compute · Underlying instance platform for self-hosted runners
- **S3**: artifact store · Distribution target for runner binaries and optional Lambda artifacts

### Build pipelines

Artifact and image build-time pipelines feeding the runtime

- [AMI builds](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/images/README.md): packer pipeline · Build-time image pipeline for Linux and Windows runner AMIs · `images/README.md`
- [Lambda builds](https://github.com/github-aws-runners/terraform-aws-github-runner/blob/main/lambdas/package.json): artifact pipeline · TypeScript/Node.js build and release pipeline for Lambda artifacts · `lambdas/package.json`

## Connections

- 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
