The workflow: init, plan, apply, destroy

Change infrastructure safely.

Beginner12 min · lesson 4 of 15

The Terraform workflow is a short, safe cycle of four commands, and its defining feature is that it always shows you what it will do before it does it. init sets up the working directory (downloads providers, configures the backend); plan computes and displays the changes without making them; apply executes them after showing the plan again; destroy tears everything down. The plan-then-apply gate is what makes Terraform safe on real infrastructure — you never change what you have not first reviewed.

THE TERRAFORM WORKFLOW
1init
download providers, set up the backend (one-time)
2plan
dry run: show exactly what would change, apply nothing
3apply
re-show the plan, confirm, then execute
4destroy
tear it all down (also plans and confirms)
Plan-then-apply is the safety gate: you never change what you have not first reviewed.
terminal
$ terraform init # one-time: download providers, initialize the backend
$ terraform plan # DRY RUN: show exactly what would change (nothing is applied)
Plan: 2 to add, 0 to change, 0 to destroy.
$ terraform apply # execute — re-shows the plan and asks to confirm
Enter a value: yes
aws_security_group.web_sg: Creation complete
aws_instance.web: Creation complete
$ terraform destroy # tear it down (also shows a plan and confirms)

Reading a plan

The plan is the most important output to learn to read — it is your last chance to catch a mistake. Each resource carries a symbol: + create, ~ change in place, - destroy, and -/+ replace (destroy then recreate — the dangerous one, meaning downtime or data loss). The summary line is your headline check. If the plan proposes to destroy or replace something you did not intend, stop and fix the code before applying.

terminal
$ terraform plan
~ resource "aws_instance" "web" {
~ instance_type = "t3.micro" -> "t3.small" # ~ = change in place (safe)
}
-/+ resource "aws_db_instance" "main" { # -/+ = DESTROY and recreate (data loss!)
~ engine_version = "14" -> "16"
}
Plan: 1 to add, 1 to change, 1 to destroy.

Save the plan you apply

In automation you separate plan from apply so the thing you reviewed is exactly the thing that runs. terraform plan -out=tfplan writes the plan to a file; terraform apply tfplan applies precisely that, with no re-computation and no second prompt. This closes the gap where infrastructure changes between the plan you approved and the apply that executes — the basis of the CI/CD pattern later.

Watch for -/+ (replace) — it destroys first
The most dangerous thing in a plan is -/+ (replace): Terraform destroys the existing resource and creates a new one, which for a database or any stateful resource means data loss and downtime. It happens when you change an argument that cannot be updated in place. Always scan the plan for replaces and destroys before typing yes; for a stateful resource, protect it (prevent_destroy) or sequence a migration — never an automatic apply.