Skip to content
Latchkey

How to Run terraform fmt and validate in GitHub Actions

A malformed module or unformatted HCL only shows up at plan time; fmt and validate catch it earlier and cheaper.

Run terraform fmt -check and terraform validate after init -backend=false so style and config errors fail the job.

Steps

  • Set up the Terraform CLI with hashicorp/setup-terraform.
  • Run terraform fmt -check -recursive to verify formatting.
  • Run terraform init -backend=false then terraform validate to check config validity without a backend.

Workflow

.github/workflows/terraform-check.yml
name: Terraform Check
on: [pull_request]
jobs:
  tf:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3
      - run: terraform fmt -check -recursive
      - run: terraform init -backend=false
      - run: terraform validate

Notes

  • Use -backend=false so validate runs without provisioning or remote state access.
  • On Latchkey managed runners Terraform checks run cheaper and self-heal if a runner dies.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →