How do you structure Terraform code for large teams?
Answer
Scaling Terraform for large teams involves several patterns. Environment separation: use separate directories (or workspaces with separate state) per environment: infra/envs/dev/, infra/envs/prod/. Each environment has its own main.tf calling shared modules. Module library: extract reusable patterns into modules (infra/modules/vpc/, infra/modules/eks/). Publish to a private module registry or maintain in the same repo. Layer separation: split infrastructure into layers (network, databases, applications) with separate state files — changes to the app layer don't risk the network layer. Remote state data sources: reference other layers' outputs via terraform_remote_state. Atlantis or Terraform Cloud: automate plan/apply in CI/CD with PR-based workflows. Naming conventions: consistent resource tagging with environment, team, and project. Linting: tflint, checkov, and tfsec in CI pipelines.