Documentation Index
Fetch the complete documentation index at: https://mintlify.com/hashicorp/terraform/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Theterraform_remote_state data source allows Terraform configurations to access output values from other Terraform configurations. This enables sharing data between separately managed infrastructure components.
Use Cases
Infrastructure Composition
Remote state is commonly used to compose infrastructure across multiple Terraform configurations:- Networking: Reference VPC IDs and subnet IDs from a network configuration
- Platform Services: Access database endpoints from a data layer configuration
- Application Deployment: Reference load balancer DNS names from infrastructure config
- Shared Resources: Access security group IDs, IAM roles, and other shared resources
Organization Patterns
Remote state enables several organizational patterns:- Layer Separation: Network, platform, and application layers managed separately
- Team Boundaries: Different teams manage different infrastructure components
- Update Isolation: Changes to one component don’t require re-planning others
- Blast Radius Reduction: Limit the scope of infrastructure changes
State Output Values
Only root module output values are accessible via remote state:internal/states/state.go:33-38
Output Value Structure
internal/states/output_value.go
Setting Output Values
Output values are set during state operations:internal/states/state.go:315-324
State Manager Output Access
Backends provide access to output values through the state manager:internal/states/statemgr/
Remote State Configuration
Local Backend Example
S3 Backend Example
Terraform Cloud Example
Output Value Best Practices
Explicit Output Declarations
Always explicitly declare outputs you intend to share:Sensitive Data Handling
Structured Outputs
Use objects for related values:State Schema Compatibility
Remote state consumers depend on output schemas:Version Compatibility
- Additive Changes: Adding new outputs is safe
- Breaking Changes: Removing or renaming outputs breaks consumers
- Type Changes: Changing output types can break consumers
Handling Schema Changes
State Access Control
Backend Permissions
Remote state access requires backend read permissions:S3 Backend
Terraform Cloud
Use workspace sharing and team permissions to control access.Anyone with read access to the state backend can access all output values, including those marked sensitive.
Alternative Patterns
While remote state is powerful, consider alternatives:Service Discovery
For dynamic infrastructure, use service discovery:Parameter Store / Secrets Manager
For configuration values:Data Sources
Query resources directly:Performance Considerations
Remote state access has performance implications:State Size
Large state files slow down remote state reads:- Keep state files focused
- Split large infrastructures into logical components
- Avoid storing large data in outputs
Network Latency
Remote backends add network latency:- Use regional backends when possible
- Consider caching strategies for CI/CD
Plan Performance
Each remote state data source is fetched during planning:Troubleshooting
Output Not Found
Backend Configuration Mismatch
Permission Denied
Best Practices
Minimize Cross-State Dependencies
Minimize Cross-State Dependencies
Keep cross-configuration dependencies minimal. Tight coupling makes refactoring difficult.
Document Output Contracts
Document Output Contracts
Treat outputs as API contracts. Document types, formats, and deprecation plans.
Version Outputs Carefully
Version Outputs Carefully
Use semantic versioning for breaking changes. Maintain backward compatibility when possible.
Avoid Sensitive Data in Outputs
Avoid Sensitive Data in Outputs
While technically possible, avoid storing highly sensitive data in state outputs. Use secrets management instead.
Use Descriptive Output Names
Use Descriptive Output Names
Choose clear, stable output names that reflect their purpose and content.
Test Cross-Configuration Changes
Test Cross-Configuration Changes
When changing outputs, test impact on all consuming configurations.
Related Resources
- State Overview - Core state concepts
- State Backends - Backend configuration
- Output Values - Defining outputs in configurations
Source Code References
- Output Values:
internal/states/output_value.go - State Structure:
internal/states/state.go:315-332 - State Manager:
internal/states/statemgr/