tofu commands to validate, format, visualize, and apply your configurations.
OpenTofu Command Overview
| Command | Purpose | Example |
|---|---|---|
tofu validate | Validate HCL syntax and internal consistency | tofu validate |
tofu fmt | Reformat HCL files to canonical style | tofu fmt |
tofu show | Display current infrastructure state | tofu show --json |
tofu providers | List providers required by configuration and state | tofu providers |
tofu output | Read defined outputs or a specific output value | tofu output pet-name |
tofu refresh | Refresh state without planning or applying changes | tofu refresh |
tofu plan | Show execution plan after refreshing state | tofu plan |
tofu graph | Generate a DOT-format dependency graph for visualization | tofu graph > graph.dot |
1. Validate Configuration
Usetofu validate to check your HCL files for syntax errors and internal consistency. It flags errors with precise file and line numbers.
When validation fails, OpenTofu highlights the incorrect attribute. For example, replace
file permission with file_permission to match HCL naming conventions.2. Format HCL Files
Thetofu fmt command enforces a consistent style across all .tf files in the current directory, handling indentation, alignment, and spacing automatically.
3. Show Infrastructure State
tofu show prints the current state stored by OpenTofu. Add --json for a machine-readable output.
4. Providers
tofu providers lists providers declared in your configuration versus those recorded in the state file.
5. Outputs
Usetofu output to inspect all declared outputs or fetch a single output by name.
6. Refresh and Plan
By default,tofu plan and tofu apply refresh the state before execution. To update only the state file without planning or applying, run:
7. Dependency Graph
Generate a dependency graph in DOT format usingtofu graph. You can then visualize it with GraphViz:
Save the DOT output (e.g.,
graph.dot) and render it with: