Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Cloud and DevOps

Terraform Best Practices

Opens with credits

A practical guide to Terraform best practices for people who manage cloud infrastructure as code. It covers structuring and formatting configuration, pinning Terraform, providers and modules, storing state remotely with locking, keeping secrets out of state, writing modules and refactoring them safely, and a review workflow with plans, tests and policy.

Written by Tauseef Fayyaz6 chapters, 24 practicesAbout 19 min20 quiz questionsReviewed 13 October 2026

What you will learn

6 chapters · about 19 min

  1. 1Structure and styleHow to lay out files, name things and format code so every Terraform change is easy to read in a pull request.4 practices · 3 min
  2. 2Versions and dependenciesHow to pin Terraform itself, providers and modules so that a plan run next month uses the same code as the one you reviewed today.4 practices · 3 min
  3. State: remote, locked and splitTerraform state is the map between your code and real resources. These practices keep it shared, safe from concurrent runs, recoverable and small enough to reason about.4 practices · 4 minOpens with credits
  4. Secrets and credentialsHow to give Terraform the credentials it needs and pass secrets to resources without leaving them in Git, logs, state or plan files.4 practices · 3 minOpens with credits
  5. Modules and refactoringWhen to write a module, how to repeat resources safely, and how to rename or move resources without Terraform destroying them.4 practices · 3 minOpens with credits
  6. Workflow, tests and policyHow changes move from a branch to production: plans in review, applying exactly what was reviewed, tests for modules and checks on deployed infrastructure.4 practices · 3 minOpens with credits

About this guide

Terraform best practices exist because infrastructure code fails differently from application code. A bad commit in an app usually means a bug you can roll back. A bad Terraform apply can delete a database, replace a load balancer that customers are hitting, or leak a password into a state file that half the company can read. The tool will do exactly what the configuration and the state say, so most of the craft is making sure both are right before anything is applied.

This guide follows the order you meet these problems. It starts with how to lay out and format configuration so reviews are easy, then how to pin Terraform, provider and module versions so a run next month behaves like a run today. The middle chapters cover state, which is the part that hurts most when it goes wrong: remote backends, locking, encryption and splitting state into safe pieces, then secrets and why marking a value sensitive is not the same as keeping it out of state. The last chapters cover modules and refactoring without destroying resources, and a workflow where every change is planned, reviewed, tested and checked against policy.

Every practice was checked against the current Terraform documentation on developer.hashicorp.com, including the official style guide, the pages on sensitive data, the S3 backend, refactoring with moved blocks, import blocks, the dependency lock file, version constraints and the testing framework. Each one tells you what to do, what to avoid and how to check it, with HCL you can adapt.

The advice applies whether you run Terraform from a laptop, from CI or through HCP Terraform. Where a practice depends on a paid feature, it says so and gives the open alternative.

Common questions

Should I commit .terraform.lock.hcl?

Yes. The Terraform docs recommend committing it so provider version changes go through code review. Run terraform init -upgrade to move to newer allowed versions, and terraform providers lock with each platform your team and CI use so checksums cover all of them.

Does sensitive = true keep secrets out of Terraform state?

No. It only hides values from CLI output and the UI. Terraform still stores them in state and plan files. To keep a secret out of state, use ephemeral values (Terraform 1.10+) and write-only arguments (1.11+) where the provider supports them.

Is DynamoDB still needed for Terraform state locking on S3?

No. The S3 backend supports a native lock file with use_lockfile = true, and the docs mark DynamoDB-based locking as deprecated and due for removal in a future minor version. You can set both while migrating.

How should I separate dev, staging and prod in Terraform?

Give each environment its own state. Without HCP Terraform, use one directory per environment with its own backend and variables, calling shared modules. With HCP Terraform, use a workspace per environment and split large ones further by component.

When should I use count versus for_each?

Use count for identical copies or to turn a resource on and off with count = var.enabled ? 1 : 0. Use for_each when each instance has its own name or settings, because instances are addressed by key and removing one does not shift the others.

How do I rename a Terraform resource without destroying it?

Add a moved block with the old address in from and the new one in to, then plan. The plan should say the resource has moved, with no create or destroy. Keep the block in shared modules so callers on older versions can upgrade safely.

Sources (8)

Checked against these, last on 13 October 2026.

  1. Style Guide, HashiCorp Terraform docsdeveloper.hashicorp.com/terraform/language/style
  2. Manage sensitive data in your configuration, HashiCorp Terraform docsdeveloper.hashicorp.com/terraform/language/manage-sensitive-data
  3. Backend block configuration overview: s3, HashiCorp Terraform docsdeveloper.hashicorp.com/terraform/language/backend/s3
  4. Dependency Lock File, HashiCorp Terraform docsdeveloper.hashicorp.com/terraform/language/files/dependency-lock
  5. Version Constraints, HashiCorp Terraform docsdeveloper.hashicorp.com/terraform/language/expressions/version-constraints
  6. Refactoring modules, HashiCorp Terraform docsdeveloper.hashicorp.com/terraform/language/modules/develop/refactoring
  7. Import existing resources, HashiCorp Terraform docsdeveloper.hashicorp.com/terraform/language/import
  8. Tests, HashiCorp Terraform docsdeveloper.hashicorp.com/terraform/language/tests

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com