This content originally appeared on DEV Community and was authored by Manthan Ankolekar
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
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
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
You can also save the plan to a file:
terraform plan -out=tfplan
Use this before applying changes to confirm what Terraform will do.
4. terraform apply
Applies the planned changes to your infrastructure.
terraform apply
Or apply a saved plan:
terraform apply tfplan
This is the command that actually provisions resources.
5. terraform destroy
Destroys all resources defined in the Terraform configuration.
terraform destroy
Be careful! This will tear down your infrastructure.
6. terraform show
Displays information about your Terraform state or saved plan.
terraform show
Useful for inspecting what’s deployed.
7. terraform output
Shows output values defined in your outputs.tf
.
terraform output
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
- Show details about a specific resource:
terraform state show aws_instance.example
Use this for debugging or manual state inspection.
9. terraform fmt
Formats Terraform configuration files (.tf
) to a canonical style.
terraform fmt
Helps maintain clean, consistent code formatting.
10. terraform version
Displays the currently installed Terraform version.
terraform version
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.
This content originally appeared on DEV Community and was authored by Manthan Ankolekar