Terraform Tutorial — Workflow Tips and Flags

1 minute read

Tips, flags, and patterns that make your Terraform workflow efficient and CI/CD-friendly.


terraform validate

Check syntax and configuration without querying APIs:

$ terraform validate
Success! The configuration is valid.

Catches errors like missing required arguments, wrong types, and invalid references. Run it before plan.


terraform fmt

Format all .tf files consistently:

# Format all files
$ terraform fmt

# Check formatting without changing files
$ terraform fmt -check

# Format recursively
$ terraform fmt -recursive

Use -check in CI/CD to enforce formatting:

- name: Check formatting
  run: terraform fmt -check -recursive

Useful Plan/Apply Flags

Flag Command Purpose
-auto-approve apply, destroy Skip confirmation prompt
-out=FILE plan Save plan for later apply
-var="key=val" plan, apply Set a variable
-var-file=FILE plan, apply Load variables from file
-target=ADDR plan, apply, destroy Target a single resource
-compact-warnings plan, apply Reduce warning noise
-parallelism=N plan, apply Number of concurrent operations (default 10)

CI/CD Workflow Pattern

# Step 1: Initialize
terraform init -input=false

# Step 2: Validate
terraform validate

# Step 3: Check formatting
terraform fmt -check -recursive

# Step 4: Plan and save
terraform plan -out=tfplan -input=false

# Step 5: (Manual approval gate)

# Step 6: Apply saved plan
terraform apply -input=false tfplan

The -input=false flag prevents Terraform from prompting for input in non-interactive environments.


Full Workflow Summary

terraform init          ← Download providers, modules, configure backend
    │
terraform validate      ← Check syntax and config
    │
terraform fmt -check    ← Verify formatting
    │
terraform plan          ← Preview changes
    │
terraform apply         ← Execute changes
    │
terraform destroy       ← Tear down (when done)

Makefile for Common Commands

.PHONY: init plan apply destroy

init:
	terraform init

plan:
	terraform plan -out=tfplan

apply:
	terraform apply tfplan

destroy:
	terraform destroy -auto-approve

fmt:
	terraform fmt -recursive

validate:
	terraform validate
$ make plan
$ make apply

What’s Next?

Practice the complete workflow with the exercises.


← terraform destroy Practice Exercises →