Using the Remote State Module
The remote-state Terraform module provides a native HCL approach to reading outputs from other Atmos components.
This method keeps all component dependencies within Terraform code rather than stack configurations.
For most use cases, !terraform.state in stack YAML is recommended because:
- It's faster (no provider initialization required)
- Dependencies are visible in stack configurations
- No additional Terraform code needed
Use the remote-state module when you prefer keeping dependencies in HCL.
You will learn
- Native Terraform/HCL approach for reading component outputs
- Uses the
cloudposse/stack-config/yaml//modules/remote-statemodule - Stack-aware - automatically resolves component context
- Keeps component dependencies in Terraform code
When to Use the Remote State Module
Use the remote-state module when you:
- Prefer keeping all component logic in Terraform HCL
- Want to use Terraform's native remote state patterns
- Need conditional remote state lookups based on Terraform logic
- Are working with teams more familiar with Terraform than YAML
Basic Usage
Add a remote-state.tf file to your component that reads outputs from other components:
components/terraform/eks/remote-state.tf
Then use the outputs in your component:
components/terraform/eks/main.tf
How It Works
The remote-state module:
- Uses the
contextto determine the current stack - Looks up the specified component's configuration in that stack
- Reads the component's Terraform state from the configured backend
- Returns all outputs as a map via
module.<name>.outputs
The module uses the terraform-provider-utils provider internally to process Atmos stack configurations.
Cross-Stack References
Read outputs from components in different stacks by overriding context variables:
components/terraform/app/remote-state.tf
Using Variables for Component Names
Make the remote state lookup configurable via variables:
components/terraform/app/variables.tf
components/terraform/app/remote-state.tf
Then configure the component in your stack:
stacks/prod.yaml
Conditional Remote State
Use Terraform's count or for_each for conditional lookups:
components/terraform/app/remote-state.tf
components/terraform/app/main.tf
atmos.yaml Configuration
The remote-state module uses the terraform-provider-utils provider, which needs to find your atmos.yaml configuration. Ensure atmos.yaml is discoverable:
The provider searches for atmos.yaml in these locations (highest to lowest priority):
ATMOS_CLI_CONFIG_PATHenvironment variable- Current working directory
~/.atmos/atmos.yaml/usr/local/etc/atmos/atmos.yaml
For CI/CD environments, set the environment variables:
export ATMOS_CLI_CONFIG_PATH=/path/to/atmos/config
export ATMOS_BASE_PATH=/path/to/repo
Complete Example
Here's a full example of a component that reads from multiple other components:
components/terraform/eks/remote-state.tf
components/terraform/eks/main.tf
Comparison with YAML Functions
| Aspect | Remote State Module | !terraform.state |
|---|---|---|
| Definition location | Terraform HCL | Stack YAML |
| Visibility | In component code | In stack configuration |
| Conditional logic | Terraform count/for_each | Not supported |
| Performance | Medium | Fastest |
| Provider required | Yes (terraform-provider-utils) | No |
Consider Using !terraform.state
For simpler use cases where you don't need Terraform-level conditionals, !terraform.state provides faster performance and keeps dependencies visible in stack configurations.
Learn More