Skip to content

About

OpenTofu/Terraform module: AWS CodeBuild-hosted GitHub Actions runner for one repository

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

terraform-aws-codebuild-runner

OpenTofu and Terraform module that creates an AWS CodeBuild-hosted GitHub Actions runner for one repository. Workflow jobs in that repository run in your VPC, so they can reach private endpoints such as a private EKS API.

Usage

Pin the module to a release tag:

module "runner" {
  source = "git::https://github.com/SlickFinch/terraform-aws-codebuild-runner.git?ref=v1.0.0"

  name                  = "acme-runner"
  github_repository_url = "https://github.com/acme/infra.git"
  credential_arn        = aws_secretsmanager_secret.github_token.arn

  vpc_id     = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets
}

In the workflow, use the label the module builds:

runs-on: codebuild-acme-runner-${{ github.run_id }}-${{ github.run_attempt }}

See examples/complete for a VPC, the secret and the apply order.

How it behaves

  • One repository. The webhook belongs to the repository in github_repository_url, and its only filter is WORKFLOW_JOB_QUEUED. GitHub sends a repository webhook only that repository's events, so jobs from other repositories never start a build here.
  • Credentials. With credential_type = "SECRETS_MANAGER", credential_arn is a secret whose value is {"ServerType":"GITHUB","AuthType":"PERSONAL_ACCESS_TOKEN","Token":"<token>"}, tagged codebuild:source:provider = github and codebuild:source:type = personal_access_token. With CODECONNECTIONS, it is a connection ARN. Changing the type is a variable change. The module never sees the token.
  • Source credential. CodeBuild keeps one GitHub source credential per account and Region. The module creates it by default. Set create_source_credential = false if something else manages it. The project also authenticates on its own.
  • Token scopes. A classic token needs repo. A fine-grained token needs Contents (read), Commit statuses (read and write), Webhooks (read and write) and Administration (read and write) on the repository.
  • Service role. It can write to its log group, manage the build's network interfaces in subnet_ids, and read the one credential. It has no sts or iam actions. Jobs get AWS access through GitHub OIDC.
  • Network. The security group has no ingress rules. Egress follows egress_cidr_blocks, on every port unless egress_tcp_ports lists the TCP ports to allow (for example [443]). The subnets need NAT to reach GitHub.

Apply order

The webhook needs a working credential when it is created. Create the secret, put the token in it, then apply the module.

Testing

mise install
tofu test            # mocked provider, no AWS credentials needed
pre-commit run --all-files

Versioning

Releases follow semantic versioning. Each release is a vMAJOR.MINOR.PATCH git tag with a GitHub release. CHANGELOG.md lists the changes in each release.

  • Patch: bug fixes. No change to inputs, outputs or resources.
  • Minor: new inputs or outputs with safe defaults.
  • Major: anything that makes callers change code, raises a provider floor, or replaces resources.

Requirements

Name Version
terraform >= 1.8.0
aws >= 6.0

Providers

Name Version
aws >= 6.0

Modules

No modules.

Resources

Name Type
aws_cloudwatch_log_group.this resource
aws_codebuild_project.this resource
aws_codebuild_source_credential.this resource
aws_codebuild_webhook.this resource
aws_iam_role.this resource
aws_iam_role_policy.this resource
aws_security_group.this resource
aws_vpc_security_group_egress_rule.this resource
aws_caller_identity.current data source
aws_partition.current data source
aws_region.current data source

Inputs

Name Description Type Default Required
credential_arn ARN of the Secrets Manager secret or the CodeConnections connection that credential_type names. string n/a yes
github_repository_url HTTPS clone URL of the one repository whose workflow jobs may run here, for example https://github.com/acme/infra.git. string n/a yes
name CodeBuild project name. Jobs pick the runner with the label codebuild---. string n/a yes
subnet_ids Private subnets the builds run in. They need a route to the internet (NAT) to reach GitHub. list(string) n/a yes
vpc_id VPC the builds run in. string n/a yes
build_timeout Minutes before CodeBuild stops a build (5 to 2160). number 60 no
compute_type CodeBuild compute type. string "BUILD_GENERAL1_SMALL" no
concurrent_build_limit Most builds that run at once. Null means the account limit. number null no
create_source_credential Create the account-wide GitHub source credential. CodeBuild keeps one per account and Region, so set false if something else manages it. The project authenticates on its own either way. bool true no
credential_type How CodeBuild authenticates to GitHub: SECRETS_MANAGER (a secret that holds a personal access token) or CODECONNECTIONS (the AWS Connector for GitHub app). string "SECRETS_MANAGER" no
egress_cidr_blocks IPv4 CIDRs the builds may send traffic to, on any port. list(string)
[
"0.0.0.0/0"
]
no
egress_tcp_ports TCP ports the builds may use toward egress_cidr_blocks. Null allows every protocol and port. list(number) null no
environment_type CodeBuild environment type, for example LINUX_CONTAINER or ARM_CONTAINER. string "LINUX_CONTAINER" no
image Build image. string "aws/codebuild/amazonlinux-x86_64-standard:5.0" no
log_retention_in_days Days CloudWatch Logs keeps build logs. number 30 no
tags Tags for every taggable resource. map(string) {} no

Outputs

Name Description
project_arn ARN of the CodeBuild project.
project_name Name of the CodeBuild project.
role_arn ARN of the CodeBuild service role.
role_name Name of the CodeBuild service role, always -codebuild.
runner_label_prefix Start of the runs-on label. Use codebuild--${{ github.run_id }}-${{ github.run_attempt }}.
security_group_id ID of the runner security group.

About

OpenTofu/Terraform module: AWS CodeBuild-hosted GitHub Actions runner for one repository

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages