Debugging: TF_LOG, Console, Validate and Format
Scenario
Your terraform plan is failing with a cryptic error. You need to see detailed logs, test expressions interactively, check syntax, and standardize formatting.
Your Objectives
- Set TF_LOG to different levels (TRACE, DEBUG, INFO, WARN, ERROR).
- Use TF_LOG_PATH to save logs to a file.
- Use terraform console to test expressions interactively.
- Use terraform validate to check syntax.
- Use terraform fmt to standardize formatting.
Additional Context
Log levels from most to least verbose: TRACE > DEBUG > INFO > WARN > ERROR. TRACE is enormous — it shows all API calls and responses. Use ERROR or WARN first, escalate to TRACE only as a last resort. Always unset TF_LOG after debugging.
main.tf (for console testing)
terraform {
required_version = ">= 1.5.0"
}
variable "servers" { default = ["web", "api", "db"] }
variable "env" { default = "dev" }
locals {
prefix = "app-${var.env}"
upper_servers = [for s in var.servers : upper(s)]
}Workflow Commands
# ─── terraform validate: Check syntax WITHOUT calling any API ───
terraform init
terraform validate
# Success! The configuration is valid.
# ─── terraform fmt: Standardize formatting ───
terraform fmt # fixes formatting in-place
terraform fmt -check # check only, exit 1 if changes needed (CI-friendly)
terraform fmt -diff # show what would change
# ─── TF_LOG: Verbose logging ───
export TF_LOG=ERROR # only errors
terraform plan
export TF_LOG=WARN # errors + warnings
terraform plan
export TF_LOG=DEBUG # detailed internal operations
terraform plan
export TF_LOG=TRACE # EVERYTHING (very verbose)
terraform plan
# Save logs to a file instead of stderr
export TF_LOG=DEBUG
export TF_LOG_PATH=/tmp/terraform-debug.log
terraform plan
cat /tmp/terraform-debug.log | head -50
# ALWAYS unset after debugging
unset TF_LOG TF_LOG_PATH
# ─── terraform console: Interactive expression tester ───
terraform console
# Inside the console:
# > var.servers
# ["web", "api", "db"]
# > local.prefix
# "app-dev"
# > upper("hello")
# "HELLO"
# > length(var.servers)
# 3
# > [for s in var.servers : "${local.prefix}-${s}"]
# ["app-dev-web", "app-dev-api", "app-dev-db"]
# > exit
✓ terraform console Session
> var.servers tolist(["web", "api", "db"]) > local.prefix "app-dev" > [for s in var.servers : upper(s)] ["WEB", "API", "DB"] > exit

