🎓 A1Learner
🔍
0 of 50 lessons completed

terraform validate, fmt and graph

Terraform validate, fmt and graph are three commands that check and tidy a configuration. Validate checks it, fmt formats it, and graph draws it.

Validate checks the syntax

A valid configuration prints a success message. An error names the file and line to fix.

terraform validate

Fmt formats the code

Fmt fixes indentation and spacing. It changes the layout, not the meaning.

terraform fmt

Graph draws dependencies

Graph shows which resource depends on which. A simple configuration has a simple graph.

terraform graph

Example

# main.tf
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "2.9.1"
    }
  }
}

resource "local_file" "hello" {
  filename = "hello.txt"
  content  = "Hello from Terraform."
}

# Commands:
# terraform init
# terraform validate
# terraform fmt
# terraform graph

Key points

  • Validate checks the syntax and the references between blocks.
  • Validate needs init first, so it can see the providers.
  • Fmt rewrites files to the standard spacing and layout.
  • Run fmt before sharing a file, so every file looks the same.
  • Graph prints the dependencies between resources as a diagram.

Validate checks the syntax and reports that the configuration is valid. Fmt tidies the spacing. Graph draws the links between resources. Together they keep a configuration clean and readable.

Note

Validate finds errors in the writing, not errors in the plan.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Which command checks that a configuration is valid?