Terraform Commands Explained: Essential Commands Guide

Published: 2025-08-12
8 min read
Share:

Most Terraform deployment issues are caught long before infrastructure is created. A missing variable, incorrect provider configuration, or outdated state file can cause failed deployments and wasted debugging time.

That's why understanding Terraform commands is just as important as learning Terraform syntax. Whether you're new to Infrastructure as Code (IaC) or already managing production environments, these commands help you validate, plan, deploy, inspect, and troubleshoot infrastructure more effectively.

If you're just getting started, check out our guide on getting started with Terraform and AWS before diving into advanced workflows.

Most Important Terraform Commands Explained for Daily Use

In a typical Terraform workflow, you'll use these commands repeatedly:

  1. terraform init
  2. terraform fmt
  3. terraform validate
  4. terraform plan
  5. terraform apply
  6. terraform output
  7. terraform show
  8. terraform destroy

Understanding when to use each command can significantly reduce deployment mistakes and improve infrastructure reliability.

Terraform Init – Initialize Your Working Directory

Before Terraform can manage infrastructure, it must initialize the working directory.

The terraform init command downloads required providers, configures backends, and prepares the working environment.

terraform init

This command is usually the first thing you run after cloning a repository or creating a new Terraform project.

If your configuration uses providers from the official Terraform Registry, Terraform automatically downloads the required plugins during initialization.

When to Use Terraform Init

  • Starting a new Terraform project
  • Cloning an existing Terraform repository
  • Adding new providers
  • Changing backend configurations

Terraform Fmt – Keep Terraform Code Consistent

Terraform provides a built-in formatting tool called terraform fmt.

It automatically formats configuration files according to Terraform's standard style conventions.

terraform fmt

Using consistent formatting improves readability and reduces unnecessary Git diffs.

Pro Tip

Run terraform fmt before committing code. Many engineering teams include it in CI/CD pipelines alongside validation checks.

Terraform Validate – Catch Configuration Errors Early

The terraform validate command checks whether your configuration is syntactically valid and internally consistent.

terraform validate

Terraform verifies resource definitions, variable references, provider configurations, and expressions without making infrastructure changes.

For example, if you reference a variable that doesn't exist or use an unsupported argument, Terraform can detect the issue before deployment.

Why Terraform Validate Matters

Many teams run validation automatically during pull request reviews because it catches mistakes before they reach production.

A common workflow looks like this:

terraform fmt
terraform validate
terraform plan

If you're working with variables, these guides may also help:

Terraform Plan – Preview Infrastructure Changes Safely

The terraform plan command creates an execution plan showing what Terraform intends to change.

terraform plan

Before creating, modifying, or deleting resources, Terraform compares your configuration with the current state and displays the proposed actions.

The output typically includes:

  • Resources to be created
  • Resources to be updated
  • Resources to be destroyed
  • Attribute-level changes

Reviewing plans carefully helps prevent accidental infrastructure changes.

Skip State Refresh During Planning

In some situations, you may choose to skip refreshing infrastructure state.

terraform plan -refresh=false

This can improve performance in large environments, but Terraform may work with outdated infrastructure information.

Terraform Apply – Create or Update Infrastructure

The terraform apply command executes the actions proposed in a Terraform plan.

terraform apply

Terraform will display the planned changes and request confirmation before proceeding.

To skip interactive approval:

terraform apply -auto-approve

Be cautious when using auto-approval in production environments. Most teams use it only in controlled CI/CD pipelines.

Terraform Show – Inspect Terraform State and Resources

The terraform show command displays information stored in Terraform state.

terraform show

This helps you inspect deployed infrastructure and understand what Terraform currently manages.

If you want to generate machine-readable output:

terraform show -json

Terraform can then integrate the output with automation tools, reporting systems, or custom scripts.

To better understand how Terraform stores infrastructure information, read our guide on how Terraform state works.

You may also find these related resources useful:

Terraform Providers – Inspect Provider Dependencies

Providers are responsible for interacting with cloud platforms and services.

The terraform providers command displays all providers referenced by your configuration.

terraform providers

This is useful when troubleshooting provider issues or auditing dependencies in large projects.

If you're learning how providers work, read:

You can also browse official provider documentation in the Terraform Registry.

Terraform Output – Retrieve Important Values

Output variables allow Terraform to expose useful information after deployment.

You can display all outputs using:

terraform output

Example outputs might include:

  • Public IP addresses
  • DNS names
  • Load balancer endpoints
  • Resource IDs

To retrieve a specific output:

terraform output instance_ip

Output values are especially useful when integrating Terraform with deployment pipelines and automation scripts.

Learn more about Terraform output variables.

Terraform Refresh – Synchronize State with Infrastructure

Historically, terraform refresh was used to update Terraform state based on existing infrastructure.

terraform refresh

Modern Terraform workflows generally rely on state refresh operations that occur during planning and applying.

As a result, standalone use of terraform refresh is less common today than it was in earlier Terraform versions.

Practical Example

Imagine an AWS resource was modified manually through the AWS Console.

Terraform's refresh mechanisms help ensure the state reflects the actual infrastructure before future changes are applied.

Terraform Graph – Visualize Resource Dependencies

Complex Terraform projects often contain dozens or hundreds of interconnected resources.

The terraform graph command generates a dependency graph in DOT format.

terraform graph

You can visualize the output using tools such as Graphviz.

terraform graph | dot -Tpng > graph.png

In large AWS environments containing VPCs, security groups, EC2 instances, load balancers, and databases, dependency graphs can quickly reveal hidden relationships that affect resource creation order.

Terraform Destroy – Remove Infrastructure Safely

The terraform destroy command removes all resources managed by the current Terraform configuration.

terraform destroy

Terraform displays a detailed plan before deleting resources.

Always review the proposed changes carefully, especially in shared or production environments.

Many organizations restrict destroy operations to specific users or automated workflows to reduce operational risk.

A Typical Terraform Workflow

Most engineers follow a workflow similar to the one below:

terraform init
terraform fmt
terraform validate
terraform plan
terraform apply
terraform output

When troubleshooting or maintaining infrastructure, additional commands such as show, graph, and providers become useful.

Terraform Commands Cheat Sheet

Core Workflow Commands

  • terraform init — Initialize working directory
  • terraform fmt — Format Terraform code
  • terraform validate — Validate configuration
  • terraform plan — Preview changes
  • terraform apply — Deploy infrastructure
  • terraform destroy — Remove infrastructure

Inspection Commands

  • terraform show — Display state information
  • terraform output — Display output values
  • terraform providers — View provider dependencies
  • terraform graph — Visualize resource relationships

Common Mistakes When Using Terraform Commands

Skipping Validation

Running terraform apply without validating configurations increases the likelihood of deployment failures.

Ignoring Plan Output

Always review the execution plan before applying changes, especially in production environments.

Manual Infrastructure Changes

Making changes outside Terraform can create state drift and unexpected deployment behavior.

Not Understanding State

Many troubleshooting issues stem from a misunderstanding of Terraform state management.

Consider reviewing:

Conclusion

Terraform commands form the foundation of every Infrastructure as Code workflow.

Commands such as init, validate, plan, and apply help deploy infrastructure safely, while tools like show, output, providers, and graph make troubleshooting and maintenance significantly easier.

As your Terraform projects grow, these commands become part of your daily workflow. Understanding what each command does—and when to use it—can save hours of debugging and help you manage infrastructure more confidently.

Frequently Asked Questions (FAQs)

What is the use of the terraform validate command?

It checks Terraform configuration files for syntax and configuration errors before deployment.

How do I automatically format Terraform files?

Run:

terraform fmt

Terraform will automatically format configuration files according to official style guidelines.

Can I skip state refresh during planning?

Yes.

terraform plan -refresh=false

This can improve performance, but Terraform may operate using outdated infrastructure information.

How do I visualize Terraform dependencies?

Use:

terraform graph

You can then render the output using Graphviz.

What is the benefit of terraform output?

It provides easy access to deployment values such as IP addresses, DNS names, endpoints, and resource IDs.

Which Terraform commands should beginners learn first?

Start with:

  1. terraform init
  2. terraform fmt
  3. terraform validate
  4. terraform plan
  5. terraform apply

These commands cover the core Terraform workflow used in most projects.

Free Engineering ToolsNEW

8 free, 100% client-side tools for developers — no signup, no data uploads.

Explore all tools