Skip to main content
Terraform CLI is the primary operational interface for Terraform—every lifecycle action (validate, plan, apply, inspect, and manage state) is executed through CLI commands. This guide covers additional, commonly used Terraform CLI commands beyond the basics, organized to match a typical workflow:
  1. Validation and formatting
  2. Reviewing deployed resources and exported values
  3. Inspecting provider usage and visualizing resource dependencies
  4. Inspecting and reconciling state
  5. Importing existing resources into your Terraform state
We’ll follow this sequence and show the purpose, typical usage, and helpful flags for each command. For the canonical Terraform documentation and version-specific details, see the official Terraform docs: https://www.terraform.io/docs

Quick reference table

1. Validation and formatting

Before creating a plan or applying changes, validate your configuration to catch errors early and format files to a consistent style.
  • Validate configuration correctness:
  • Format files recursively to the standard style:
Run terraform fmt and terraform validate in CI pipelines and pre-commit hooks to ensure consistent formatting and to detect configuration issues before they reach shared environments.
Run terraform fmt and terraform validate from the root of your configuration directory so they operate across the entire workspace. Integrating these commands into CI or a pre-commit hook reduces diff noise and catches mistakes early.

2. Reviewing deployed resources and exported values

After you create a plan or have state, use terraform show to inspect it and terraform output to read exported values.
  • Create a plan and save it to a file for later inspection:
  • Show a saved plan:
  • Show the current state in a human-friendly format:
  • Read outputs:
    • List all outputs:
    • Get a single output value:
    • Get outputs in JSON (machine-readable) form:
Use terraform show when you need to inspect the contents of a saved plan or the in-memory plan after terraform plan. Use terraform output to expose configured output blocks to external systems (CI scripts, other tooling) and prefer -json for automation.

3. Providers and graph

Discover which providers your configuration references and visualize resource dependency relationships.
  • List providers referenced by the configuration:
This command helps you locate provider addresses (for example registry.terraform.io/hashicorp/aws) and see which modules consume them.
  • Generate a dependency graph (Graphviz required to render the output):
terraform graph outputs DOT format describing relationships between resources and modules. Piping to dot (part of Graphviz) produces a visual diagram you can inspect to understand resource dependencies and module boundaries. Tip: Large graphs can be noisy—filter or focus on modules to make diagrams actionable.

4. State and refresh

Terraform’s state commands let you inspect and manage the state file. Because state is authoritative for Terraform-managed resources, operate on it carefully and always back up before making destructive changes.
  • List resources tracked in the state:
  • Show a specific state resource’s attributes:
  • Pull the remote state contents (raw JSON):
  • Refresh the state by reconciling Terraform state with real-world infrastructure and preview differences:
Note: Older Terraform versions included a terraform refresh command to update state in place; current workflows commonly prefer terraform plan -refresh-only to preview changes before applying them. When changing state directly (for example using terraform state mv, terraform state rm, or manual edits), always make a backup of the state file or rely on backend versioning.
Be cautious when manipulating state directly. Always back up your state (or enable backend versioning) before performing state modifications such as terraform state rm or terraform state mv. Mistakes in state can lead to resource drift or accidental destruction.

5. Importing existing resources

If you have existing infrastructure created outside Terraform, import it into a Terraform state and then author the matching configuration blocks.
  • Basic import syntax:
  • Example (import an AWS EC2 instance):
After running terraform import, Terraform adds the resource and its attributes to the state, but you must create a corresponding resource block in your .tf files that matches the imported resource. Run terraform plan afterwards to identify any attribute or configuration gaps, then update your configuration to align with the imported state. Tips for importing:
  • Write the resource block first (with the same resource type and name) so the import has a clear address.
  • For complex resources, consider importing smaller logical pieces first (for example, security groups or IAM roles) and then grouping them.
  • Use terraform state show <ADDRESS> to inspect attributes after import.

This lesson outlined additional Terraform CLI commands used to validate and standardize configurations, inspect and export values, explore providers and dependency graphs, manage and reconcile state, and import existing resources. Incorporate these commands into a disciplined workflow—local checks, CI validation, and careful state management—to keep your infrastructure-as-code reliable and maintainable. Links and references:

Watch Video