Terraform Tutorial — terraform plan
terraform plan shows you exactly what Terraform will do — create, update, or destroy — before making any changes.
Running plan
$ terraform plan
Terraform will perform the following actions:
# aws_s3_bucket.main will be created
+ resource "aws_s3_bucket" "main" {
+ arn = (known after apply)
+ bucket = "my-app-bucket"
+ id = (known after apply)
+ region = "us-east-1"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Reading the Plan Output
| Symbol | Meaning | Color |
|---|---|---|
+ |
Create | Green |
~ |
Update in place | Yellow |
-/+ |
Destroy and recreate | Red/Green |
- |
Destroy | Red |
Example with updates:
# aws_instance.web will be updated in-place
~ resource "aws_instance" "web" {
~ instance_type = "t3.micro" -> "t3.small"
# (other attributes unchanged)
}
Plan with Variable Files
$ terraform plan -var-file="environments/prod.tfvars"
Saving a Plan
Save the plan to a file and apply it later:
# Save the plan
$ terraform plan -out=tfplan
# Apply the saved plan (no confirmation needed)
$ terraform apply tfplan
This is critical for CI/CD pipelines:
planstage:terraform plan -out=tfplan- Manual approval step
applystage:terraform apply tfplan
Plan Exit Codes
| Exit Code | Meaning | Use in CI/CD |
|---|---|---|
| 0 | No changes needed | Skip apply |
| 1 | Error occurred | Fail the pipeline |
| 2 | Changes detected | Proceed to apply |
Use -detailed-exitcode to get exit code 2:
$ terraform plan -detailed-exitcode
$ echo $? # 0, 1, or 2
Targeting Specific Resources
Plan for a single resource:
$ terraform plan -target=aws_s3_bucket.main
⚠️ Warning: Targeting skips resources that depend on the targeted one. Use only for debugging, not regular workflow.
What’s Next?
You’ve reviewed the plan. Let’s execute it with terraform apply.