Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- Prepare: right folder, right workspace or environment, right cloud account:
- Check the code before you plan:
- Plan into a file and read the summary:
terraform plan -out=tfplan
Plan: 2 to add, 1 to change, 1 to destroy.| Symbol in the plan | Means |
|---|---|
+ | create |
~ | change in place |
-/+ or +/- | replace (delete and create — data on it is lost) |
- | destroy |
<= | read (data source) |
- Look closely at every destroy and replace — list them from the saved plan:
terraform show -json tfplan | jq -r '.resource_changes[] | select(.change.actions | index("delete")) | .address + " " + (.change.actions | join(","))'
aws_db_instance.main delete,createA database or a disk that would be replaced is the classic accident. Find why (a changed name, engine version, subnet) before you go on.
- Apply the saved plan:
terraform apply tfplan
Apply complete! Resources: 2 added, 1 changed, 1 destroyed.- Guard rails in the code:
resource "aws_db_instance" "main" {
identifier = "shop-prod"
deletion_protection = true
lifecycle {
prevent_destroy = true
}
}Also: commit .terraform.lock.hcl (same provider versions for everyone), pin provider versions, one state per environment, -target only in emergencies.
- In a team: plan in CI, apply after approval. On a pull request CI runs
planand posts the result; after the merge, a protected job (manual approval, short-lived cloud credentials via OIDC) runsapplyon the saved plan.
Check that it worked
Run plan again right after the apply — it must say:
terraform plan
No changes. Your infrastructure matches the configuration.Common problems
| Problem | Cause | Fix |
|---|---|---|
Saved plan is stale | The state changed after the plan (another apply). | Plan again and review again. |
Error acquiring the state lock | Another run is active or crashed. | Wait; force-unlock only when sure no run is active. |
| Plan shows changes nobody made | Drift: someone changed things in the console. | Decide: put the change into code, or let Terraform set it back. |
| A resource is replaced, not changed | A "force new" attribute changed. | Read the # forces replacement line; avoid the change or plan a migration. |
Inconsistent dependency lock file | Provider versions differ from the lock file. | terraform init -upgrade on purpose, then commit the new lock file. |
| Apply ran in the wrong account | Wrong profile or credentials. | Check aws sts get-caller-identity; set allowed_account_ids in the provider. |
Go further
- Command pages: terraform, jq, aws, trivy.
- Next how-tos: Store Terraform state in S3 with locking, Import an existing resource, Create a CI pipeline in GitHub Actions, Find drift, Write a module, Scan Terraform code with Checkov.
- Learn it: Terraform — The workflow, Terraform — Plan, Terraform — Apply, Terraform — lifecycle, Terraform in a pipeline.
- Full guides: Terraform, Checkov, Terragrunt.
- Practise: Lab: a full configuration with variables, locals and outputs.
1Why use terraform plan -out=tfplan and then terraform apply tfplan?
The apply does exactly what you reviewed; if the state changed in between, Terraform refuses (stale plan).
2What does -/+ mean in a plan, and why is it dangerous?
Replace: the object is deleted and created again — data on it (a database, a disk) can be lost.