1. Several environments #
Folders, tfvars or workspaces.
Comparison #
| Approach | Advantages | Limits |
|---|---|---|
| Folder per environment | Strong isolation, separate backends | Some duplication (reduced by modules) |
| tfvars files | One codebase, values per environment | Same backend: be careful |
| CLI workspaces | Several states for the same code | Easy to pick the wrong workspace; no permission isolation |
Example #
envs/
recette/ main.tf backend.tf recette.tfvars
prod/ main.tf backend.tf prod.tfvars
modules/
reseau/ app/ base/
cd envs/prod && terraform plan -var-file=prod.tfvars2. Splitting into layers #
Smaller states.
Layers #
data "terraform_remote_state" "reseau" {
backend = "s3"
config = { bucket = "etat-terraform-123", key = "reseau/prod.tfstate", region = "eu-west-3" }
}
module "app" {
source = "../../modules/app"
subnet_ids = data.terraform_remote_state.reseau.outputs.private_subnets
}3. Complementary tools #
Reducing repetition.
Tools #
- Terragrunt: DRY configuration of backends and dependencies between layers.
- HCP Terraform: remote workspaces with variables, permissions and runs per team.
- Reading outputs through data sources (SSM parameters, tags) rather than terraform_remote_state to reduce coupling.
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Terraform best practices: structure, state, security, review and maintenance
Terraform best practices for teams: repository structure, naming, pinned versions, encrypted and ...
Terraform modules: designing, versioning and reusing
Structure Terraform with modules: root module and child modules, inputs and outputs, sources (loc...
Terraform state: remote backends, locking, import and refactoring
Manage Terraform state: the role of the state file, sensitive data, remote backends (S3, azurerm,...
Preparing for the HashiCorp Certified: Terraform Associate certification
Preparation guide for the Terraform Associate: format, official objectives (IaC concepts, workflo...
Terraform: principles, architecture and workflow
Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), ...
The HCL language: resources, variables, outputs, data and expressions
Writing Terraform: terraform, provider, resource and data blocks, typed variables with validation...
Testing and securing Terraform: validate, terraform test, tflint, Checkov and policies
Ensure the quality of Terraform code: fmt and validate, native terraform test tests with mocked p...
Terraform in CI/CD: pipelines, plan review, OIDC and GitOps
Automate Terraform: plan pipeline on merge requests and apply after approval, posting the plan as...
Terraform across multiple clouds: AWS, Azure, Google Cloud, OVHcloud and Kubernetes
Use Terraform with the main providers: aws, azurerm, google, ovh and openstack, kubernetes and he...