DevOps Peach

Advanced Terraform · Module 03

Environments, Import and Drift

Run the same code for dev, staging and prod with workspaces or separate folders, know which variable value wins, bring existing resources under management with import blocks, and catch drift with refresh-only plans, scheduled checks and check blocks.

Practice interactively →

What you will learn

Lessons

  1. Workspaces or One Folder per Environment

    CLI workspaces keep several states for one configuration; separate folders give each environment its own code, backend and permissions. Most teams use folders for environments.

  2. Environment Layout and Variable Precedence

    Keep logic in versioned modules, keep each environment a thin root folder with its own tfvars and backend, and know the order in which variable values override each other.

  3. Importing Existing Resources

    import blocks bring objects created by hand under Terraform management through a normal plan and apply, and -generate-config-out writes a first draft of their configuration.

  4. Detecting and Handling Drift

    Drift is any difference between real infrastructure and state; find it with refresh-only plans and scheduled -detailed-exitcode runs, decide whether to accept or revert it, and add check blocks for ongoing health.