AWS Builder Center
GitHub Actions Runners Explained: Configuring Self-Hosted CI/CD Runners on AWS EC2

GitHub Actions Runners Explained: Configuring Self-Hosted CI/CD Runners on AWS EC2

Learn how to configure secure, self-hosted GitHub Actions runners on Amazon EC2 for automated CI/CD pipelines. Explore private VPC networking, AWS IAM and OIDC authentication, Systems Manager, Docker builds, Terraform, security best practices, monitoring, and autoscaling with ephemeral runners for production-ready DevOps workflows on AWS.

AWS Community Builder | GitHub Actions | Amazon EC2 | CI/CD | DevOps | AWS IAM | Terraform



1. Introduction

Continuous Integration and Continuous Deployment (CI/CD) are essential components of modern DevOps engineering.
GitHub Actions provides a workflow automation platform that allows developers to build, test, scan, and deploy applications directly from GitHub repositories.
But every GitHub Actions job needs a compute environment where its commands can execute.
That compute environment is called a runner.
GitHub provides managed runners, but organizations can also operate their own runners on Amazon EC2.
Self-hosted runners are especially useful when teams require custom software, private AWS network access, specialized hardware, or tighter control over the build environment.
This article explains the runner concept and provides a practical implementation on AWS.

2. What Is a GitHub Actions Runner?

A GitHub Actions runner is an application that receives and executes jobs from GitHub Actions workflows.
A workflow is defined using YAML files stored in:
.github/workflows/
When a configured event occurs, GitHub evaluates the workflow and schedules jobs on suitable runners.
Developer
|
v
Git Push / Pull Request
|
v
GitHub Repository
|
v
GitHub Actions Workflow
|
v
GitHub Actions Runner
|
+--> Checkout Code
+--> Install Dependencies
+--> Run Tests
+--> Build Artifacts
+--> Security Scanning
+--> Deploy Application
The runner provides the execution environment; GitHub Actions coordinates workflow scheduling and job execution.

3. GitHub-Hosted vs Self-Hosted Runners

GitHub Actions supports two primary runner approaches.

GitHub-hosted runners

GitHub manages the underlying execution infrastructure.
Benefits:
  • Minimal infrastructure management
  • Preconfigured runner images
  • Straightforward workflow setup
  • Automatic environment provisioning
  • Suitable for many common CI/CD tasks
Example:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Build started"

Self-hosted runners

Your organization manages the execution environment.
A self-hosted runner can run on:
  • Amazon EC2
  • On-premises servers
  • Virtual machines
  • Containers in supported runner orchestration architectures
  • Other supported operating systems and infrastructure
Benefits include custom software, hardware control, and access to approved private infrastructure.
However, your team becomes responsible for patching, isolation, scaling, monitoring, and runner lifecycle management.

4. Runner Comparison

Feature
GitHub-hosted
Self-hosted on EC2
Infrastructure management
GitHub
Your team
Custom machine configuration
Limited by offering
Full control
Private VPC access
Requires connectivity solution
Native VPC placement
OS patching
Managed runner image
Customer responsibility
Scaling
GitHub-managed options
Customer-designed
EC2 costs
Not applicable
EC2 and related AWS charges
Custom instance hardware
Offering-dependent
EC2 instance selection
Security maintenance
Shared platform responsibility
Greater customer responsibility
Self-hosted runners are not automatically cheaper. Compare EC2, EBS, networking, idle capacity, operations, and GitHub billing implications.

5. GitHub Actions Runner Architecture on AWS

Developer
|
v
GitHub Repository
|
v
GitHub Actions
|
Job Scheduling
|
v
AWS EC2 Instance
Self-Hosted Runner
|
+------------+------------+
| | |
v v v
Git Clone Build/Test Deploy
| | |
+------------+------------+
|
v
AWS IAM Role
|
v
AWS Services
ECR / S3 / EKS / EC2
The runner initiates outbound connections to GitHub to receive jobs.
It does not normally require an inbound public port for GitHub to dispatch workflow jobs.

6. Recommended AWS Architecture

For production environments, consider a private-subnet deployment.
GitHub Cloud
|
HTTPS / TLS
|
Approved Egress Path
|
AWS Private Subnet
|
v
Amazon EC2 Runner
|
+-----------+-----------+
| | |
v v v
ECR S3 EKS
| | |
+-----------+-----------+
|
v
AWS Resources
A private EC2 instance still needs access to GitHub's required endpoints.
S3 and other AWS VPC endpoints do not replace connectivity to GitHub.
Possible egress designs include NAT, an approved outbound proxy, or another controlled connectivity solution.
Use Systems Manager for administration instead of exposing SSH publicly.

7. AWS Prerequisites

Before configuring the runner, prepare:
  • AWS account and VPC
  • EC2 instance
  • Linux operating system
  • Suitable EBS storage
  • IAM instance profile for required AWS operations
  • Outbound HTTPS connectivity to GitHub
  • Systems Manager access
  • GitHub repository or organization administration rights
  • Runner registration permissions
For a demonstration, Amazon Linux 2023 is a practical choice.
For production, choose an EC2 instance size based on build requirements and concurrency.

8. Launch Amazon EC2

Example configuration:
Instance Name: github-actions-runner
Operating System: Amazon Linux 2023
Instance Type: t3.medium
Root EBS: 30 GiB gp3
Subnet: Private Subnet
Public IP: Disabled
Security Group: Restricted Egress
IAM Role: Runner Instance Profile
Administration: AWS Systems Manager
This is an example, not a universal sizing recommendation.
CPU-intensive builds, Docker images, Java applications, and large dependency trees may require larger instances.
Configure IMDSv2 and encrypted EBS storage.

9. Connect to EC2 Using Systems Manager

Open the AWS Systems Manager console and connect through Session Manager.
The instance needs:
  • SSM Agent
  • An appropriate IAM instance profile
  • Connectivity to required SSM endpoints
  • Proper security group and endpoint configuration
For a private subnet without NAT, configure the necessary Systems Manager interface VPC endpoints.
Do not expose SSH to the internet solely to manage the runner.

10. Install Required Packages

On Amazon Linux 2023:
sudo dnf update -y

sudo dnf install -y \
git \
curl \
tar \
gzip \
jq \
unzip \
docker
Enable Docker if workflows require container builds:
sudo systemctl enable --now docker
Only grant Docker access to trusted runner identities. Docker daemon access is effectively privileged host access.

11. Create a Dedicated Runner User

Avoid running build jobs as root.
sudo useradd --create-home --shell /bin/bash githubrunner

sudo mkdir -p /opt/actions-runner

sudo chown githubrunner:githubrunner /opt/actions-runner
Switch to the runner user:
sudo -iu githubrunner
cd /opt/actions-runner
The runner should execute under a dedicated account with the minimum permissions needed.

12. Download the GitHub Actions Runner

In GitHub:
Repository → Settings → Actions → Runners → New self-hosted runner
Select Linux and the correct CPU architecture.
GitHub provides commands for the current runner release.
Example command structure:
mkdir -p ~/actions-runner
cd ~/actions-runner

curl -fL -o actions-runner.tar.gz \
"https://github.com/actions/runner/releases/download/<VERSION>/actions-runner-linux-x64-<VERSION>.tar.gz"
Replace  <VERSION>  with the actual version shown in GitHub.
Verify the release archive checksum using the value provided by GitHub before extraction.
tar xzf actions-runner.tar.gz
Do not copy an old runner download URL from a tutorial without checking the latest supported release.

13. Register the Runner with GitHub

GitHub provides a short-lived registration token.
Example:
./config.sh \
--url https://github.com/ORG/REPO \
--token YOUR_SHORT_LIVED_REGISTRATION_TOKEN \
--name aws-ec2-runner-01 \
--labels aws,ec2,linux \
--work _work \
--unattended
Replace  ORG ,  REPO , and the token with the actual values.
Registration tokens are sensitive and expire.
Do not commit them to Git, paste them into public logs, or embed them permanently in user data.
After registration, GitHub should show the runner in the repository's runner list.

14. Run the Runner

For a foreground test:
./run.sh
A successful connection should show the runner listening for jobs.
For production, use the service installation scripts provided with the runner package.
The service scripts generally require elevated permissions for installation, while the configured runner process should execute as the dedicated runner user.
Example:
sudo ./svc.sh install githubrunner
sudo ./svc.sh start
sudo ./svc.sh status
Run these commands from the actual runner installation directory.

15. Create Your First GitHub Actions Workflow

Create:
.github/workflows/ec2-runner-test.yml
Example:
name: EC2 Self-Hosted Runner Test

on:
workflow_dispatch:

jobs:
test:
runs-on: [self-hosted, linux, x64, aws, ec2]

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Show runner information
run: |
hostname
uname -a
df -h

- name: Verify Git
run: git --version

- name: Run build
run: echo "Build executed on AWS EC2"
Make sure the runner labels match the labels used in  runs-on .
The  x64  label applies only if the runner is registered on an x86-64 instance.

16. Configure AWS Authentication

There are two common approaches.

Option A: EC2 instance profile

The EC2 instance receives temporary AWS credentials through its attached IAM role.
This is convenient but requires careful permissions because jobs executing on the host may be able to access the instance credentials.

Option B: GitHub Actions OIDC

GitHub Actions can request short-lived credentials by assuming an AWS IAM role through OpenID Connect.
OIDC is often preferable for separating deployment permissions by repository, environment, or workflow.
However, OIDC alone does not isolate an untrusted workflow from the underlying EC2 runner.
The host still needs strong isolation and a carefully restricted instance profile.

17. GitHub Actions OIDC Deployment Example

Configure the AWS IAM OIDC identity provider and a role trust policy restricted to the intended GitHub repository and environment.
Example workflow:
name: Deploy to AWS

on:
workflow_dispatch:

permissions:
id-token: write
contents: read

jobs:
deploy:
runs-on: [self-hosted, linux, x64, aws, ec2]

environment: production

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/GitHubDeployRole
aws-region: ap-south-1

- name: Verify AWS identity
run: aws sts get-caller-identity
Use your actual AWS account ID and IAM role ARN.
For production, pin third-party actions to reviewed commit SHAs where practical and restrict the IAM trust policy to the intended repository and branch or environment.

18. Deploy Applications to Amazon ECR

A self-hosted runner can build Docker images and push them to Amazon ECR.
Example flow:
GitHub Push
|
v
Self-Hosted EC2 Runner
|
v
Docker Build
|
v
Security Scan
|
v
Amazon ECR
|
v
Amazon ECS / EKS
The runner needs appropriate AWS permissions and access to the ECR endpoints.
For a fully private VPC, configure required ECR interface endpoints and S3 connectivity as applicable.
Do not grant unrestricted ECR or administrator permissions to the runner.

19. Runner Groups and Labels

GitHub runner groups help control which repositories may use a collection of self-hosted runners.
Labels help workflows select runners with specific capabilities.
Examples:
Runner Groups:
production-runners
development-runners
security-scanning-runners

Runner Labels:
aws
ec2
linux
docker
java
terraform
Use runner groups to restrict access, especially for production deployment infrastructure.
Labels alone should not be treated as a security boundary.

20. Persistent vs Ephemeral Runners

Persistent runner

A persistent runner remains registered and executes multiple jobs over time.
Advantages:
  • Simple initial configuration
  • Reuses installed dependencies
  • Suitable for trusted internal workloads
Risks:
  • Residual files between jobs
  • Credential contamination
  • Build environment drift
  • Persistence after a malicious job

Ephemeral runner

An ephemeral runner is designed to process a single job and then be removed.
GitHub Job Queued
|
v
Launch EC2 Runner
|
v
Register Runner
|
v
Execute One Job
|
v
Upload Logs / Artifacts
|
v
Terminate EC2
Ephemeral runners are generally preferable for stronger job isolation, particularly when workloads have different trust levels.
Ensure runner logs are exported before termination.

21. Autoscaling GitHub Runners on EC2

A production runner platform can use event-driven scaling.
GitHub Workflow Job Event
|
v
Authenticated Webhook Receiver
|
v
AWS Lambda
|
v
Runner Provisioning Logic
|
v
EC2 Instance Launch
|
v
Runner Registration
|
v
Execute Workflow
|
v
Runner Cleanup
|
v
Terminate Instance
Production scaling requires reliable webhook validation, job-to-runner matching, registration token handling, concurrency control, retries, and cleanup.
AWS Auto Scaling Groups alone do not automatically understand GitHub Actions job queues.

22. Security Best Practices

Self-hosted runners execute code supplied by workflows.
Treat that code as potentially dangerous, especially when pull requests or third-party repositories are involved.
Recommended controls:
  • Avoid running untrusted public pull requests on privileged persistent runners.
  • Use ephemeral runners for sensitive environments.
  • Separate development and production runner groups.
  • Restrict repository access to runner groups.
  • Use least-privilege IAM roles.
  • Prefer OIDC for deployment credentials.
  • Avoid static AWS access keys.
  • Restrict outbound network access where practical.
  • Patch the operating system and runner application.
  • Use encrypted EBS volumes.
  • Use Systems Manager instead of public SSH.
  • Prevent access to unnecessary instance metadata credentials.
  • Protect build artifacts and secrets.
  • Monitor runner activity and lifecycle events.
A dedicated runner user does not provide a strong security boundary against malicious workflow code by itself.

23. Monitoring and Troubleshooting

Monitor:
  • Runner online/offline status
  • Job queue time
  • Job execution duration
  • EC2 CPU and memory
  • EBS free space
  • Docker disk usage
  • Runner service health
  • Network connectivity to GitHub
  • Failed workflow jobs
  • Runner registration failures
  • EC2 instance cost
Useful Linux commands:
sudo systemctl status actions.runner.*
df -h
free -h
sudo journalctl -u 'actions.runner.*' --no-pager
The actual systemd unit name depends on the runner registration.
Inspect the runner's  _diag  directory for diagnostic logs.

24. Common Runner Issues

Runner appears offline: Check service status, outbound HTTPS, DNS, and GitHub connectivity.
Job remains queued: Verify runner labels, group access, repository permissions, and runner availability.
Permission denied: Check Linux file ownership, Docker permissions, and AWS IAM policies.
AWS credentials fail: Verify OIDC trust conditions, workflow permissions, and role assumptions.
Disk full: Clean old build files and Docker artifacts, or use disposable runners.
Runner does not update: Review automatic update settings and supported runner versions.
Private subnet cannot reach GitHub: Configure approved egress; AWS service endpoints alone are insufficient.

25. Cost Optimization

EC2 runner costs depend on:
  • Instance type
  • Running hours
  • EBS volumes
  • Data transfer
  • NAT or proxy infrastructure
  • Monitoring
  • Build concurrency
Cost strategies include:
  • Stop unused development runners.
  • Use ephemeral instances for bursty workloads.
  • Right-size CPU and memory.
  • Evaluate EC2 Spot for interruption-tolerant jobs.
  • Use predictable runner images.
  • Remove unnecessary artifacts.
  • Monitor idle time and cost per successful job.
Compare total operating cost against GitHub-hosted runner pricing and operational requirements.

26. Practical Project: Enterprise GitHub Runner Platform

Build a reusable self-hosted runner platform on AWS.

Technology stack

  • GitHub Actions
  • Amazon EC2
  • Amazon Linux 2023
  • AWS IAM
  • GitHub OIDC
  • AWS Systems Manager
  • Amazon CloudWatch
  • AWS Lambda
  • Amazon EventBridge
  • Terraform
  • Amazon ECR
  • Amazon ECS or EKS

Implementation phases

Phase 1 — Foundation
Create VPC, private subnets, security groups, IAM roles, and Systems Manager access.
Phase 2 — Runner installation
Install dependencies, download the official runner, and register it.
Phase 3 — Workflow integration
Create build and test workflows targeting the EC2 runner.
Phase 4 — AWS authentication
Configure OIDC and least-privilege deployment roles.
Phase 5 — Security
Restrict runner groups, harden the host, and define job trust boundaries.
Phase 6 — Monitoring
Add CloudWatch dashboards and runner health alerts.
Phase 7 — Scaling
Implement ephemeral runner provisioning and cleanup.
Phase 8 — Production deployment
Use approved runners to deploy applications to AWS services.

27. Production Checklist

  • EC2 instance is sized for expected builds.
  • EBS encryption and IMDSv2 are enabled.
  • Runner has outbound connectivity to GitHub.
  • Public inbound SSH is not required.
  • Systems Manager administration is configured.
  • Runner software is obtained from official GitHub releases.
  • Release checksum is verified.
  • Runner executes under a dedicated non-root user.
  • Runner groups restrict repository access.
  • Labels match workflow requirements.
  • AWS IAM permissions follow least privilege.
  • OIDC trust conditions are restricted.
  • Production workflows use approval gates.
  • Untrusted workflows cannot access privileged runners.
  • Runner service health is monitored.
  • Disk cleanup and patching are automated.
  • Ephemeral runner cleanup is tested.
  • Cost and scaling policies are documented.

28. Key Takeaway

GitHub Actions runners are the execution engines behind CI/CD workflows.
GitHub-hosted runners offer convenience and managed infrastructure, while self-hosted runners on Amazon EC2 provide greater control over compute resources, installed tools, network placement, and AWS integration.
The core architecture is:
GitHub Repository → GitHub Actions Workflow → EC2 Self-Hosted Runner → Build/Test → AWS Deployment
For enterprise environments, the strongest approach combines:
Private EC2 + Systems Manager + IAM OIDC + Runner Groups + Ephemeral Runners + Monitoring + Infrastructure as Code
The most important security principle is to treat runner-executed workflow code as a trust boundary and avoid allowing untrusted jobs to access privileged infrastructure.

Official References

Final Thought: A self-hosted runner is not just an EC2 instance running CI/CD commands. It is a critical part of the software supply chain and should be engineered with the same security, reliability, and automation standards as production infrastructure.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article