Skip to content

Terraform (infrastructure as code)

Scope: managing infrastructure with Terraform or OpenTofu (tofu instead of terraform on the command line), from single projects to many teams sharing modules. For rules about AWS itself, see AWS; for pipelines that run Terraform, see GitHub Actions.

Entries are sorted by score from 1 to 10 (criteria): 9โ€“10 start here or strongly recommended, 6โ€“8 recommended, 1โ€“5 further reading or conditional.

Adopted references

Key rules

State and secrets

Structure and modules

Versions and delivery

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

  • 7 terraform fmt -check, terraform validate and terraform test: the built-in checks. fmt -check exits non-zero when files aren't formatted, validate checks that the configuration is syntactically valid and internally consistent, and test runs the module's test files. Checks TF-12.
  • 6 TFLint Terraform ruleset: its recommended rules require required_version, provider version constraints, versions for registry modules and pinned Git module sources. It doesn't check ~> bounds or the lock file. Partly checks TF-10.
  • 6 Checkov Terraform policies: misconfiguration checks on HCL or plan JSON, including module sources without a pinned hash or version tag (CKV_TF_1, CKV_TF_2) and hard-coded AWS keys in the provider (CKV_AWS_41). Checks TF-13, and partly TF-2 and TF-10.
  • 6 HCP Terraform policy enforcement: evaluates Sentinel, OPA or Terraform policies on every run, and health assessments detect drift on a schedule. Checks TF-13 and TF-14.
  • 5 Trivy Terraform scanning: an alternative to Checkov that scans HCL and plans for misconfigurations and secrets. Partly checks TF-2 and TF-13.
  • 5 Dependabot for Terraform: opens pull requests for new provider and module versions. Partly checks TF-10.

Tutorials and further reading

  • 7 Terraform tutorials (HashiCorp): official hands-on tutorials, from getting started to modules, state, policy and automation.
  • 6 Terraform: Up & Running (Yevgeniy Brikman, book): state, modules, testing and team workflows in depth.
  • 5 Terraform Best Practices (Anton Babenko): a free community guide to code structure and naming.