Skip to main content
OpenTofu provides a powerful CLI for managing infrastructure with HCL files. In this guide, we’ll cover the essential tofu commands to validate, format, visualize, and apply your configurations.

OpenTofu Command Overview

1. Validate Configuration

Use tofu 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

The tofu 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

Use tofu 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:
This syncs your state with external changes. Then check for drift or planned changes:

7. Dependency Graph

Generate a dependency graph in DOT format using tofu graph. You can then visualize it with GraphViz:
Save the DOT output (e.g., graph.dot) and render it with:

References

Watch Video