Essential Terraform CLI Commands You Should Know
Manthan Ankolekar

Manthan Ankolekar @manthanank

About: Software Developer | JavaScript | Angular | Nodejs | MongoDB | Express.js | Python | Firebase | MySQL | Postgresql |

Location:
Karnataka, India
Joined:
Feb 21, 2021

Essential Terraform CLI Commands You Should Know

Publish Date: Aug 20
2 0

Terraform is one of the most popular Infrastructure as Code (IaC) tools, allowing developers and DevOps engineers to define and provision cloud resources in a declarative way. The Terraform CLI (Command Line Interface) is the primary way to interact with Terraform — from initializing projects to applying infrastructure changes.

In this blog, we’ll go through the most commonly used Terraform CLI commands with examples.


1. terraform init

The very first command you run in a new Terraform project.
It initializes the working directory, downloads the required provider plugins, and sets up the backend.

terraform init
Enter fullscreen mode Exit fullscreen mode

👉 Run this whenever you start a new project or modify providers/backends.


2. terraform validate

Validates the configuration files in your project to ensure there are no syntax or logical errors.

terraform validate
Enter fullscreen mode Exit fullscreen mode

👉 Helps catch issues early before running plan or apply.


3. terraform plan

Generates an execution plan showing what actions Terraform will take (create, update, destroy resources).

terraform plan
Enter fullscreen mode Exit fullscreen mode

You can also save the plan to a file:

terraform plan -out=tfplan
Enter fullscreen mode Exit fullscreen mode

👉 Use this before applying changes to confirm what Terraform will do.


4. terraform apply

Applies the planned changes to your infrastructure.

terraform apply
Enter fullscreen mode Exit fullscreen mode

Or apply a saved plan:

terraform apply tfplan
Enter fullscreen mode Exit fullscreen mode

👉 This is the command that actually provisions resources.


5. terraform destroy

Destroys all resources defined in the Terraform configuration.

terraform destroy
Enter fullscreen mode Exit fullscreen mode

👉 Be careful! This will tear down your infrastructure.


6. terraform show

Displays information about your Terraform state or saved plan.

terraform show
Enter fullscreen mode Exit fullscreen mode

👉 Useful for inspecting what’s deployed.


7. terraform output

Shows output values defined in your outputs.tf.

terraform output
Enter fullscreen mode Exit fullscreen mode

👉 Great for retrieving information like IP addresses, instance IDs, etc.


8. terraform state

Manages the Terraform state file. Common subcommands:

  • List resources in state:
  terraform state list
Enter fullscreen mode Exit fullscreen mode
  • Show details about a specific resource:
  terraform state show aws_instance.example
Enter fullscreen mode Exit fullscreen mode

👉 Use this for debugging or manual state inspection.


9. terraform fmt

Formats Terraform configuration files (.tf) to a canonical style.

terraform fmt
Enter fullscreen mode Exit fullscreen mode

👉 Helps maintain clean, consistent code formatting.


10. terraform version

Displays the currently installed Terraform version.

terraform version
Enter fullscreen mode Exit fullscreen mode

👉 Always good to verify when collaborating across teams.


Quick Reference Table

Command Purpose
terraform init Initialize project & download providers
terraform validate Validate config files
terraform plan Preview execution plan
terraform apply Apply infrastructure changes
terraform destroy Destroy resources
terraform show Show state or plan
terraform output Display output values
terraform state Manage state file
terraform fmt Format configuration files
terraform version Show Terraform version

Conclusion

Terraform CLI commands are the backbone of working with Infrastructure as Code. By mastering these commands, you’ll be able to confidently manage cloud resources across providers like AWS, Azure, and GCP.

Whether you’re just starting with Terraform or already deploying production workloads, these commands should be part of your daily toolkit.


Comments 0 total

    Add comment