What you will learn in this TP
- Write a Terraform configuration
- Use plan, apply and state
- Detect and fix drift
Introduction #
What you will learn in this section
- Write a Terraform configuration
- Use plan, apply and state
- Detect and fix drift
Context. Test environments are built by hand and never look alike. You describe them as code, using the Docker provider to work locally without a cloud account.
Prerequisites: Docker and Terraform (or OpenTofu) installed · Estimated time: 40 min
A reproducible environment and a drift detected then fixed.Hands-on #
Guided steps, with their solution.
1. Write the configuration
Why? The configuration describes the desired state; Terraform computes the actions.
Iniac-lab/, writemain.tf: a network, an nginx image and a variable number of containers exposed on 8081, 8082…, with an output listing the URLs.2. Initialise, check, plan
Why? fmt and validate are the CI checks; the plan is what you review in the pull request.
Initialise the project, check format and validity, then save a plan.3. Apply and inspect state
Why? State links code to real resources.
Apply the plan, test the URLs, then list and inspect state resources.4. Change the infrastructure
Why? You change a variable, not commands: the plan shows the exact difference.
Move to 3 instances with-var, review the plan then apply.5. Detect drift
Why? A manual change makes the code wrong: Terraform detects and fixes it.
Delete a container by hand, detect the drift with-detailed-exitcode(code 2), then re-apply.6. Destroy
Why? An environment described as code can be removed and recreated without forgetting anything.
Destroy everything, check nothing remains, then explain what team work would require (remote backend, locking, CI).
Difficulty level : ●●○○○ (2/5)
My progress
Sign in to track your progress and take notes. Sign in
Recommended Articles
Infrastructure as Code et GitOps : Terraform, Ansible et Argo CD
Gérer l'infrastructure comme du code : déclaratif ou impératif, Terraform (providers, état, plan,...