Multi-Environment Terraform Directories vs Workspaces
Parent: MongoDB Atlas Infrastructure as Code · Published reference · snapshot 2026-10-24 · skill mongodb-cohort-4308c7661d27
↓ Facts as markdown↓ Portable .llms packagent context libraries
Source-anchored research for Multi-Environment Terraform Directories vs Workspaces. Run status: COMPLETED.
These notes link each claim to its source. Some citations identify archived research reports that are no longer hosted on this site. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.
Definitions
- For Atlas infrastructure, one defensible design (a synthesis, not a vendor prescription) is one environment root configuration and state boundary per application/environment project, reusing shared modules. MongoDB recommends separate production projects per application and environment pair, and Google's Terraform best practices document environment-specific root configurations that reference shared modules. Sources: https://www.mongodb.com/docs/atlas/architecture/current/hierarchy.md ; https://docs.cloud.google.com/docs/terraform/best-practices/root-modules [source 1] [source 2] [source 3]
Structure and components
- Environment directories have maintenance costs. Terragrunt reduces repeated backend configuration through inherited settings and generated backend files, but adds another tool and configuration layer; its directory approach still requires more environment files than CLI workspaces. (low confidence) [source]
How it works
- MongoDB recommends separate Atlas projects for each application and environment pair, particularly in production, because project-level configurations such as network and security settings define isolation and least-privilege boundaries. Mapping separately controlled Terraform deployments to the relevant Atlas projects is an architectural inference, not a MongoDB-mandated repository layout. Sources: https://www.mongodb.com/docs/atlas/architecture/current/hierarchy.md [source 1] [source 2]
How-to and procedures
- Avoid treating workspace switches as system decomposition: components requiring separate configurations and backends should be separated explicitly. Google additionally warns that extensive terraform.workspace conditionals can make configuration harder to read. (high confidence) Sources: https://developer.hashicorp.com/terraform/cli/workspaces ; https://cloud.google.com/docs/terraform/best-practices/root-modules [source 1] [source 2] [source 3]
Problems, failure modes and limitations
- A Terraform 1.3.9 issue reported that setting TF_WORKSPACE without a cloud workspace mapping still produced "Missing workspace mapping strategy". Treat this as a historical integration report, not proof of a current defect or general CLI workspace failure. (low confidence) [source]
Comparisons and alternatives
- HCP Terraform workspaces differ from CLI workspaces: each contains its own configuration, variables and state, and permissions can be granted to individual users and teams per workspace. HashiCorp recommends splitting large monolithic configurations into smaller ones, each in its own workspace (for example networking-prod, app1-prod, monitoring-prod), which also makes it easy to reuse configurations for other environments. Sources: https://developer.hashicorp.com/terraform/cloud-docs/workspaces [source 1] [source 2]
- HCP Terraform workspaces differ from CLI workspaces: each has its own configuration, variables, state and run history, and workspace permissions can delegate access to teams. (high confidence) Sources: https://developer.hashicorp.com/terraform/cloud-docs/workspaces ; https://developer.hashicorp.com/terraform/cloud-docs/users-teams-organizations/permissions/workspace [source 1] [source 2]
- A directory-per-environment pattern can keep root configurations small: declare each environment's backend and instantiate shared modules with environment-specific inputs instead of duplicating resource definitions. Google Cloud explicitly recommends this layout and using only the default CLI workspace in each environment directory. (high confidence) [source 1] [source 2]
- CLI workspaces remain useful for temporary feature-branch environments using the same configuration and backend. They reuse a working copy and plugin/module caches, reducing overhead compared with maintaining separate working directories. (low confidence) [source]
Facts and statements
- Terraform CLI workspaces keep separate state instances within one working directory while reusing its configuration and backend; selecting a workspace changes which state most Terraform commands operate on. (high confidence) [source 1] [source 2]
- For deployments requiring separate credentials and access controls, HashiCorp recommends separate configurations, each with its own backend, rather than CLI workspaces, because CLI workspaces within a working directory share the same backend and are not a suitable isolation mechanism. Sources: https://developer.hashicorp.com/terraform/cli/workspaces [source 1] [source 2] [source 3]
- Environment directories can avoid wholesale configuration duplication: place reusable infrastructure in child modules and instantiate them from dev, qa, and prod roots, each declaring its own backend location and inputs. (high confidence) [source 1] [source 2]
- CLI workspaces remain useful for temporary parallel copies and tests of substantially identical infrastructure. Rejecting every use of workspaces overstates the isolation guidance. (high confidence) [source 1] [source 2]
- Terraform CLI workspaces separate state within one working directory and backend; they do not by themselves provide different backend credentials or access-control boundaries between environments. (high confidence) [source 1] [source 2]
- Directory-based deployments can pin different module releases in development, staging and production, allowing promotion after testing. Gruntwork demonstrates this with separate Terragrunt configurations using Git source refs; changing a shared local module path alone does not provide independent version pinning. (low confidence) [source]
Children
- No children recorded.