Home/Refreshers/Terraform and OpenTofu: refresher
Terraform and OpenTofu
A tool that creates and changes cloud resources to match .tf files. OpenTofu is its open-source fork, and takes the same files with tofu in place of terraform.
Remember
- Three commands do most of the work: init, plan, apply. Plan changes nothing.
- The state file is Terraform's memory of what it made. It contains secrets in plain text.
- terraform.tfstate never goes in Git. .terraform.lock.hcl always does.
- Team state lives in a shared bucket that is encrypted, versioned, and locked.
- In a plan: + creates, ~ changes, - destroys, and -/+ replaces.
Commands
| Download providers | terraform init |
| Preview | terraform plan |
| Make it so | terraform apply |
| Show outputs | terraform output |
| Move local state to a backend | terraform init -migrate-state |
| Clear a stuck lock | terraform force-unlock <lock-id> |
| Remove everything the folder manages | terraform destroy |
Watch out
- "forces replacement" next to a small edit means delete and recreate. Stop and read.
- destroy removes everything the folder manages, including things you were not thinking about.
- A provider with no version constraint can change behaviour with no commit.
Check it worked
Read the plan's last line first: how many to add, change, and destroy. Stop if destroy is above zero and you did not expect it.