CD3: Terraform foundations

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

Versions, providers and secrets

🎯You'll be able to

Pin Terraform and provider versions, commit the lock file, and generate secrets in Terraform instead of writing them in files.

🏠The everyday picture

A pharmacist dispenses "paracetamol 500 mg, batch 2611" rather than "some painkiller". The exact batch means the next refill behaves the same, and a recall reaches exactly the right strips.

📘What it really is

📝A worked example

Input: replace POSTGRES_PASSWORD=routerani123 in the starter's main.tf with random_password.db.result, add the random provider with a version, run terraform init and terraform apply.

Expected output: init records hashicorp/random in .terraform.lock.hcl; the plan shows random_password.db to create and the password as (sensitive value). git grep routerani123 finds nothing in infra/. Project 1 test 2 now passes its password and version checks.

🌍Where it's used

📝Case scenario

A logistics company's Terraform had no provider version. One Monday, a pipeline picked up a new major version of the cloud provider whose defaults had changed, and the plan wanted to replace 30 load balancer rules. A careful reviewer stopped it. They pinned the provider, committed the lock file, and upgraded later in a planned change with the release notes open.

🖱️Try it

Run terraform version and terraform providers in infra/, then open .terraform.lock.hcl. Success: you can name the exact provider versions in use and the constraint each one satisfies.

⚠️Common mistakes

🛠️For your project

Project 1 test 2 checks that every provider has a version, no image uses latest, and no database password is written in a .tf file.

✅Quick check

What does ~> 4.6 allow?

Show the answer

Version 4.6 and later 4.x releases, but not 5.0.

Why commit .terraform.lock.hcl?

Show the answer

So everyone and every pipeline uses the exact same provider builds.

← PreviousChanging things safely
ProLearners Assist