Terraform Core Commands
Four commands form the backbone of every Terraform workflow. You use them in almost every project, every day. This topic explains what each command does, when to run it, and what output to expect.
The Terraform Workflow at a Glance
Write .tf files
|
v
terraform init ← Download providers and set up the project
|
v
terraform plan ← Preview changes before touching anything
|
v
terraform apply ← Create or update real infrastructure
|
v
terraform destroy ← Remove all resources when no longer needed
Think of these four commands as: Set up → Preview → Build → Clean up.
terraform init
The first thing you always run in a new Terraform project is terraform init. This command prepares your working directory.
What init Does
- Reads your
.tffiles to find out which providers you declared - Downloads those provider plugins from the Terraform Registry
- Creates a hidden
.terraformfolder to store downloaded plugins - Creates a
.terraform.lock.hclfile that records the exact plugin versions used
When to Run init
- When starting a new project
- When you add a new provider to your configuration
- When a teammate has updated the provider versions
terraform init
Sample Output
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 5.0"... - Installing hashicorp/aws v5.31.0... Terraform has been successfully initialized!
terraform plan
Before making any real changes, terraform plan shows you exactly what Terraform intends to do. No changes happen at this stage — it is purely a preview.
What plan Does
- Reads your current
.tffiles - Reads the current state of your infrastructure (from the state file)
- Compares the two and calculates the difference
- Prints a list of actions: what will be created, changed, or deleted
Reading the Plan Output
+ resource will be CREATED ~ resource will be UPDATED in place - resource will be DESTROYED -/+ resource will be DESTROYED and re-created
A + means something new appears. A - means something disappears. A ~ means something changes but stays.
terraform plan
Saving a Plan to a File
terraform plan -out=myplan.tfplan
Saving the plan is important in CI/CD pipelines. You generate the plan, a human reviews it, and then you apply that exact saved plan — ensuring no surprises.
terraform apply
Once you review the plan and confirm it looks correct, terraform apply executes the changes against real infrastructure.
terraform apply
Terraform shows the plan again and asks:
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes
Type yes and press Enter. Terraform contacts the cloud provider APIs and creates or modifies your resources.
Skipping the Prompt (Automation)
terraform apply -auto-approve
Use this flag in automated pipelines where no human is present to type yes. Never use it blindly — always know what the plan contains before automating approval.
Applying a Saved Plan
terraform apply myplan.tfplan
When you apply a saved plan file, Terraform skips the prompt entirely and applies exactly what was planned.
terraform destroy
When you no longer need the infrastructure, terraform destroy removes everything Terraform created.
terraform destroy
This is invaluable for learning environments and temporary test setups. You spin up infrastructure, test it, then destroy it — paying only for the time it ran.
Destroying a Specific Resource
terraform destroy -target=aws_instance.web_server
The -target flag limits the destroy operation to one specific resource rather than the entire configuration.
Other Useful Commands
| Command | What It Does |
|---|---|
terraform validate | Checks your configuration for syntax errors without contacting any provider |
terraform fmt | Automatically formats your .tf files to follow standard style |
terraform show | Displays the current state or a saved plan in human-readable format |
terraform output | Prints the current output values from your state |
Key Points
terraform initsets up the project and downloads provider plugins — always the first step.terraform planpreviews changes safely — nothing is created or destroyed at this stage.terraform applyexecutes the plan and creates real infrastructure.terraform destroyremoves all managed resources — essential for cleanup.- Always run
terraform fmtandterraform validatebefore committing code to version control.
