Skip to content

Commit

Permalink
feat: add CodeBuild GitHub Runner for status checks (#284)
Browse files Browse the repository at this point in the history
Add Terraform to manage a CodeBuild project.  This project will provide a
serverless GitHub runner for the Upptime status check workflow.

The goal is to have the runner executing in AWS infrastructure as it is less
likely that it will be blocked by the AWS WAF managed IP reputation rule group.
  • Loading branch information
patheard authored Feb 10, 2025
1 parent 8772ec1 commit 48b2fcc
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "status-statut",
"image": "mcr.microsoft.com/devcontainers/base:bullseye@sha256:f485b76ec2971017849252b494e61f4b7d97cd75d094db8161c34343edc026e9",
"containerEnv": {
"SHELL": "/bin/zsh"
},
"features": {
"ghcr.io/devcontainers/features/aws-cli:1": {
"version": "latest"
},
"ghcr.io/devcontainers/features/terraform:1": {
"version": "1.10.5",
"terragrunt": "0.72.9"
}
},
"customizations": {
"vscode": {
"extensions": [
"hashicorp.terraform",
"github.copilot",
"github.vscode-github-actions",
"hashicorp.hcl"
]
}
},
"remoteUser": "vscode"
}
42 changes: 42 additions & 0 deletions .github/workflows/terraform-apply.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Terraform apply"

on:
push:
branches:
- main
paths:
- "terraform/**"
- ".github/workflows/terraform-apply.yml"

env:
AWS_REGION: ca-central-1
CONFTEST_VERSION: 0.27.0
TERRAFORM_VERSION: 1.10.5
TERRAGRUNT_VERSION: 0.72.9
TF_INPUT: false
TF_VAR_github_personal_access_token: ${{ secrets.CODEBUILD_RUNNER_PERSONAL_ACCESS_TOKEN }}

permissions:
id-token: write
contents: read

jobs:
terraform-apply:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Setup terraform tools
uses: cds-snc/terraform-tools-setup@v1

- name: Configure AWS credentials using OIDC
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: arn:aws:iam::283582579564:role/status-statut-apply
role-session-name: TFApply
aws-region: ${{ env.AWS_REGION }}

- name: Terragrunt apply
working-directory: "terraform"
run: terragrunt apply --terragrunt-non-interactive -auto-approve
48 changes: 48 additions & 0 deletions .github/workflows/terraform-plan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: "Terraform plan"

on:
pull_request:
branches:
- main
paths:
- "terraform/**"
- ".github/workflows/terraform-plan.yml"

env:
AWS_REGION: ca-central-1
CONFTEST_VERSION: 0.27.0
TERRAFORM_VERSION: 1.10.5
TERRAGRUNT_VERSION: 0.72.9
TF_INPUT: false
TF_VAR_github_personal_access_token: ${{ secrets.CODEBUILD_RUNNER_PERSONAL_ACCESS_TOKEN }}

permissions:
id-token: write
contents: read
pull-requests: write

jobs:
terraform-plan:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Setup terraform tools
uses: cds-snc/terraform-tools-setup@v1

- name: Configure AWS credentials using OIDC
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: arn:aws:iam::283582579564:role/status-statut-plan
role-session-name: TFPlan
aws-region: ${{ env.AWS_REGION }}

- name: Terragrunt plan
uses: cds-snc/terraform-plan@d79bcf0eccf632a0ad9e9193072b42c970766c5b # v3.3.1
with:
directory: "terraform"
comment-delete: "true"
comment-title: "Terraform: GitHub Runner :octocat:"
github-token: "${{ secrets.GITHUB_TOKEN }}"
terragrunt: "true"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Terraform
.terragrunt-cache
.terraform
*.tfstate
*.tfvars
25 changes: 25 additions & 0 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions terraform/codebuild.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module "github_runner" {
source = "github.com/cds-snc/terraform-modules//codebuild_github_runner?ref=v10.3.0"

project_name = "cds-snc-status-statut"
github_repository_url = "https://github.com/cds-snc/status-statut.git"
github_personal_access_token = var.github_personal_access_token

billing_tag_value = "SRE"
}
12 changes: 12 additions & 0 deletions terraform/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = var.region
}
26 changes: 26 additions & 0 deletions terraform/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
locals {
region = "ca-central-1"
}

terraform {
source = "."
}

inputs = {
region = local.region
}

remote_state {
backend = "s3"
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
config = {
encrypt = true
bucket = "status-statut-tf"
use_lockfile = true
region = local.region
key = "./terraform.tfstate"
}
}
10 changes: 10 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "github_personal_access_token" {
description = "The GitHub personal access token to use for the CodeBuild project"
type = string
sensitive = true
}

variable "region" {
description = "The AWS region to deploy to"
type = string
}

0 comments on commit 48b2fcc

Please sign in to comment.