Skip to main content

Run Terraform from GitHub Actions

Report an Issue

This guide shows how to run the Teleport Terraform provider in a GitHub Actions workflow that authenticates to Teleport with the job's ID token, so that no Teleport credential is stored in GitHub.

You will create a Machine & Workload Identity bot that can manage only Teleport roles with a specific label, run a workflow that creates such a role with Terraform, and see the change in the audit log.

Workshop environment

The workshop environment simulates a GitHub Enterprise Server at https://github.localhost:11443, with a sample repository that contains the workflow and Terraform template from this guide, and a runner with Terraform installed.

How it works​

The workflow never holds a Teleport credential. Teleport's Terraform provider joins directly with an embedded version of tbot, so the workflow doesn't need a dedicated tbot step.

NamePurpose
BotA Machine ID identity for the workflow. Its role defines which Teleport resources Terraform can manage.
Join tokenA github join token that names the bot and the repository, branch, and workflow allowed to join. It is not a secret.
ID tokenA JWT that GitHub issues to each job that has id-token: write permission. Its claims include the repository, ref, and workflow.
Teleport Terraform providerRequests the job's ID token, exchanges it for short-lived certificates for the bot, and manages Teleport resources with them.
Remote state backendKeeps Terraform state outside the job, so each run sees what earlier runs created.

Teleport checks the ID token's signature and its claims against the join token's allow rules. The certificates are valid for at most an hour and are never written to the runner, so there is nothing to rotate or revoke.

Prerequisites​

  • A Teleport cluster.
  • A Teleport user with the editor role, and tctl signed in as that user.
  • A GitHub repository where you can add workflows. GitHub-hosted runners must be able to reach the Teleport Proxy Service, and terraform.releases.teleport.dev to download the Teleport provider.
  • A Terraform remote backend for the state, such as an Amazon S3 bucket, that the workflow can access. This guide uses an S3 bucket and an IAM role that the workflow assumes through GitHub's OIDC provider in AWS.
Workshop environment
  • Sign in to https://teleport.localhost:3080 and choose Andrew Admin. This user is in the admins group, which grants editor.
  • The simulated GitHub Enterprise Server hosts the repository example-org/teleport-k8s-demo, with the workflow .github/workflows/terraform.yaml and the template terraform/main.tf.
  • The environment creates the bot, role, and join token from this guide on startup. Read steps 1 and 2 to see how they're configured, then run the workflow in step 3.

Step 1/4. Review Teleport resources​

First, we create a Teleport role that lets the bot manage Teleport roles.

The role's where clause limits which roles the bot can create, update, or delete to those labeled managed-by: terraform.

kind: role
version: v7
metadata:
  name: terraform-gha-demo
spec:
  allow:
    rules:
      # The provider reads a role before creating it.
      - resources: [role]
        verbs: [list, read]
      # Write access only to roles with the label.
      - resources: [role]
        verbs: [create, update, delete]
        where: 'equals(resource.metadata.labels["managed-by"], "terraform")'

Next, we define a bot that can use this role:

kind: bot
version: v1
metadata:
  name: terraform-gha-demo
spec:
  roles:
    - terraform-gha-demo

Lastly, we create a join token (of type github) that defines which workflow runs can join as the bot. Limiting the token to the Terraform workflow means that other workflows in the same repository can't act as this bot.

kind: token
version: v2
metadata:
  name: terraform-gha-demo
spec:
  roles: [Bot]
  bot_name: terraform-gha-demo
  join_method: github
  github:
    allow:
      # Only workflows in this repository...
      - repository: example-org/teleport-k8s-demo
        # ...running on the main branch...
        ref_type: branch
        ref: refs/heads/main
        # ...from the workflow named Terraform.
        workflow: Terraform

Save the three resources in a file named terraform-demo-resources.yaml, separated by lines containing ---, and create them:

tctl create -f terraform-demo-resources.yaml
Workshop environment

The workshop's join token also sets enterprise_server_host: github.localhost:11443 under github, so that Teleport trusts ID tokens signed by the simulated GitHub Enterprise Server instead of github.com.

Step 2/4. Review the workflow and Terraform template​

The Terraform template (terraform/main.tf) configures the Teleport provider to join as the bot and creates a simple role with the managed-by: terraform label:

terraform {
  required_providers {
    teleport = {
      source  = "terraform.releases.teleport.dev/gravitational/teleport"
      version = "19.0.2"
    }
  }

  # Keep state outside the job.
  backend "s3" {
    bucket = "example-terraform-state"
    key    = "teleport/terraform.tfstate"
    region = "us-east-1"
    # Lock the state with S3, so overlapping runs can't corrupt it.
    # Requires Terraform 1.10 or later.
    use_lockfile = true
  }
}

provider "teleport" {
  addr        = "teleport.localhost:3080"
  # Join with the job's GitHub Actions ID token instead of a stored identity.
  join_method = "github"
  join_token  = "terraform-gha-demo"
}

resource "teleport_role" "terraform_demo" {
  version = "v7"
  metadata = {
    name        = "terraform-demo"
    description = "Managed by Terraform from a GitHub Actions workflow"
    # The bot may only create, update, or delete roles with this label.
    labels = { "managed-by" = "terraform" }
  }
  spec = {
    allow = { logins = ["terraform-demo"] }
  }
}

The workflow (.github/workflows/terraform.yaml) gets credentials for the state backend, installs Terraform, and runs terraform init and terraform apply on the template:

name: Terraform
on:
  push:
    branches: [main]
  workflow_dispatch:
jobs:
  apply:
    runs-on: ubuntu-latest
    permissions:
      # Lets the job request a GitHub OIDC ID token.
      id-token: write
      contents: read
    steps:
      - uses: actions/checkout@v7
      # Assumes an IAM role with the job's ID token, to access the state.
      - uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: arn:aws:iam::123456789012:role/terraform-state
          aws-region: us-east-1
      - uses: hashicorp/setup-terraform@v4
      - run: terraform -chdir=terraform init -input=false
      - run: terraform -chdir=terraform apply -auto-approve -input=false

The id-token: write permission is required. Without it, GitHub doesn't issue an ID token and the provider can't join. The workflow's name must match the workflow rule in the join token.

The configure-aws-credentials step uses the same ID token to assume an IAM role through GitHub's OIDC provider in AWS, so the job can read and write the state without a stored AWS secret either. See the S3 backend documentation for the permissions that this role needs.

Workshop environment
  • The workshop's template pins the provider to the version of the workshop's Teleport cluster.
  • Instead of a remote backend, the template uses backend "local" {}, and the workflow sets the state file's path with -backend-config="path=${TF_STATE_PATH:-terraform.tfstate}". The runner keeps that file in data/terraform-state in the workshop directory, so it persists across runs and runner restarts.
  • Because the state stays on the runner, the workshop's workflow has no configure-aws-credentials step.

Step 3/4. Run the workflow​

Open the repository's Actions tab, select Terraform, and click Run workflow on the main branch. In the workshop environment, open https://github.localhost:11443/example-org/teleport-k8s-demo/actions.

The terraform init step installs the Teleport provider. The terraform apply step joins Teleport, creates the role, and prints Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

To see the change, open the audit log in the Teleport Web UI. Each run has Bot Joined events (bot.join) with the method github, whose attributes show the repository, ref, workflow, and actor from the ID token. The provider joins more than once per run, so you see more than one event. Next is a Role Created event (role.created) for terraform-demo, by the user bot-terraform-gha-demo. To see the role from the command line:

tctl get role/terraform-demo

The role has the label managed-by: terraform and the login terraform-demo.

Step 4/4. Run the workflow again​

Run the Terraform workflow again. The state says the role already exists, so Terraform prints No changes. The bot still joins, but there is no role event.

Change the role​

In terraform/main.tf, add a login to the role:

    allow = { logins = ["terraform-demo", "terraform-extra"] }

Commit the change to main and run the workflow. Terraform updates the role in place and prints Apply complete! Resources: 0 added, 1 changed, 0 destroyed. The audit log shows the update as another Role Created event (role.created), and tctl get role/terraform-demo lists both logins.

Workshop environment

The runner copies the repository from the workshop directory (github/repo/) into each job, so you can edit github/repo/terraform/main.tf and run the workflow without a commit. To undo the change, remove "terraform-extra" and run the workflow again.

Try a branch that isn't allowed​

Run the workflow on another branch, such as dev. The join token only allows refs/heads/main, so Teleport rejects the join and the terraform apply step fails with id token claims did not match any allow rules. The role and the state are unchanged, and the audit log shows a failed bot.join event.

Further reading​