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 -recursiveto verify formatting. - Run
terraform init -backend=falsethenterraform validateto 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 validateNotes
- Use
-backend=falseso 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
How to Scan IaC with Checkov in GitHub ActionsScan Terraform and other IaC in GitHub Actions with Checkov so misconfigurations like public buckets fail CI…
How to Run Terraform in a Reusable Workflow in GitHub ActionsWrap terraform plan and apply in a reusable workflow_call workflow so every environment calls the same audite…