Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 50IaC · Terraform

How to plan and apply Terraform safely

terraform apply can create, change — and destroy — real infrastructure in seconds. Most Terraform incidents are not bugs, they are an unread plan. The safe workflow is: format and validate, save the plan to a file, read it (especially every destroy and replace), apply exactly that file, and in a team let CI do it with a review. This how-to shows that workflow and the guard rails that stop accidents.

  • 5 Lessen
  • 4 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

50.1

The quick answer

terraform fmt -check -recursive
terraform validate
terraform plan -out=tfplan
terraform show tfplan
terraform apply tfplan

apply tfplan applies exactly what you reviewed — nothing that changed in between.

50.2

Step by step

  1. Prepare: right folder, right workspace or environment, right cloud account:
terraform init
terraform workspace show
aws sts get-caller-identity --query Account --output text
  1. Check the code before you plan:
terraform fmt -recursive
terraform validate
tflint
trivy config .
  1. 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 planMeans
+create
~change in place
-/+ or +/-replace (delete and create — data on it is lost)
-destroy
<=read (data source)
  1. 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,create

A database or a disk that would be replaced is the classic accident. Find why (a changed name, engine version, subnet) before you go on.

  1. Apply the saved plan:
terraform apply tfplan
Apply complete! Resources: 2 added, 1 changed, 1 destroyed.
  1. Guard rails in the code:
main.tf (part)
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.

  1. In a team: plan in CI, apply after approval. On a pull request CI runs plan and posts the result; after the merge, a protected job (manual approval, short-lived cloud credentials via OIDC) runs apply on the saved plan.

50.3

Check that it worked

Run plan again right after the apply — it must say:

terraform plan
No changes. Your infrastructure matches the configuration.

50.4

Common problems

ProblemCauseFix
Saved plan is staleThe state changed after the plan (another apply).Plan again and review again.
Error acquiring the state lockAnother run is active or crashed.Wait; force-unlock only when sure no run is active.
Plan shows changes nobody madeDrift: someone changed things in the console.Decide: put the change into code, or let Terraform set it back.
A resource is replaced, not changedA "force new" attribute changed.Read the # forces replacement line; avoid the change or plan a migration.
Inconsistent dependency lock fileProvider versions differ from the lock file.terraform init -upgrade on purpose, then commit the new lock file.
Apply ran in the wrong accountWrong profile or credentials.Check aws sts get-caller-identity; set allowed_account_ids in the provider.

50.5

Go further

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.

Alle commando's in dit hoofdstuk