Sharing resources between Terraform workspaces
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Terraform is a widely used Infrastructure as Code (IaC) tool that allows users to define and provision infrastructure using configuration files. While Terraform can manage a vast range of providers, one powerful feature is its ability to use workspaces. Workspaces facilitate managing different environments (e.g., development, staging, production) within a single configuration by defining isolated collections of resources.
In many scenarios, there's a need to share resources between these workspaces. This article delves into strategies for sharing resources between Terraform workspaces, providing technical details, examples, and best practices.
Understanding Terraform Workspaces
Terraform workspaces enable you to manage different states of the same configuration. This is particularly useful for scenarios like separating environments or managing multi-tenant infrastructure. Each workspace has its own state file, but they share the same set of Terraform configurations.
Key Characteristics of Terraform Workspaces
- Isolation: Each workspace has its own state, providing an isolated environment.
- Configuration Sharing: All workspaces share the same configuration, providing consistency.
- Environment Management: Useful for managing different environments (e.g., dev, test, prod) within the same project.
Sharing Resources Across Workspaces
While isolation is a key benefit, there are valid needs to share resources between workspaces. One approach is cross-workspace resource sharing. This entails using existing resources managed by one workspace (e.g., production) in another (e.g., development).
Strategies for Sharing Resources
- Resource Outputs and Data Sources:
- Use the
outputfeature to expose information about resources from one workspace. - In another workspace, use the
datablock to reference these outputs.
- Remote State Backend:
- Workspaces can be configured to store Terraform's state files in a remote backend such as AWS S3, HashiCorp Consul, or Terraform Cloud.
- Utilize the
terraform_remote_statedata source to extract resource information from a different workspace's state.
- Shared Modules and Provider Configs:
- Create modules encapsulating resource definitions that multiple workspaces use.
- Use shared provider configurations in modules to specify and consume existing resources.
Example: Using terraform_remote_state
Suppose you have a networking workspace (network
) that defines a VPC, and you want another application workspace (app
) to utilize this VPC.
network
Workspace
- Modularization: Leverage Terraform modules to encapsulate reusable infrastructure components, promoting DRY principles.
- Versioning: Maintain version control over shared resources and modules to ensure stability and predictability in shared infrastructure.
- Consistency in State Management: Ensure that your Terraform state configuration is consistent and monitors state drift.
- Security Considerations: Consider permissions and access control for state files, especially when stored in remote backends like S3.
- Multi-Cloud Environments: Workspaces can reside in different cloud providers. When sharing resources, ensure network routes and permissions are properly managed.
- Terraform Cloud/Enterprise: These provide features like remote run and variable management, facilitating easier sharing of resources across workspaces.
- State Locking: Enable locking in backends like Consul to avoid conflicts during simultaneous operations across workspaces.
Related reading
- Ship an application with a database
- Ship an application with a database
- Should dependencies between Helm charts reflect dependencies between microservices?
- Should Health Checks call other App Health Checks
- Should I have separate containers for Flask, uWSGI, and nginx?
- Should I take ILogger, ILoggerT, ILoggerFactory or ILoggerProvider for a library?
- Should I use Amazon's AWS Virtual Private Cloud VPC
- Should I use AWS Elastic Beanstalk or the Amazon EC2 Container Service ECS to scale Docker containers?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.