CD3: Terraform foundations

Lesson 4 of 5 · reading 15–25 minutes; practice and project time is on the module page

Changing things safely

🎯You'll be able to

Read a plan's symbols, spot replacements and deletions before they happen, and use lifecycle settings to change things without downtime or data loss.

🏠The everyday picture

A dentist explains the plan before starting: "this filling we'll repair; this tooth we'll take out and replace with an implant". You'd want to hear "take out" before they start, not after.

📘What it really is

📝A worked example

Input: in the finished kit, run terraform plan -var app_version=1.0.1 (after building routerani:1.0.1).

Expected output: the app copies show +/- (create replacement and then destroy), with name and image marked # forces replacement. terraform apply -var app_version=1.0.1 starts two new copies, waits for them to be healthy, then removes the old ones. In the starter (no create_before_destroy), the same change shows -/+: the only copy is removed first, so requests fail while the new one starts.

🌍Where it's used

📝Case scenario

An e-commerce engineer renamed a database resource in Terraform to tidy the code. The plan showed -/+ on the production database, but it was one line among 40 and they didn't notice. The apply deleted the database; restoring the night's snapshot took four hours and lost the morning's orders. The team added prevent_destroy, a pipeline step that fails any plan deleting a database, and the moved block for renames (CD4).

🖱️Try it

Add lifecycle { prevent_destroy = true } to docker_container.db in your copy and run terraform plan -destroy. Remove it again afterwards. Success: Terraform refuses with an error naming the resource, and you've seen how the guard works.

⚠️Common mistakes

🛠️For your project

Project 1 test 9 replaces the database and checks bookings survive, and Project 2 relies on create_before_destroy for deploys with no failed requests.

✅Quick check

What's the difference between -/+ and +/- in a plan?

Show the answer

-/+ destroys first, then creates; +/- creates the replacement first, then destroys the old one.

What does prevent_destroy do?

Show the answer

Makes any plan that would destroy that resource fail.

← PreviousState, drift and why state is secret
ProLearners Assist