Using !terraform.state
The !terraform.state YAML function is the recommended and fastest way to read Terraform/OpenTofu outputs
from other components. It retrieves outputs directly from the configured backends
without the overhead of initializing Terraform, downloading providers, or generating configuration files.
You will learn
- 10-100x faster than
!terraform.output- no Terraform initialization required - Supports all major backends: S3, GCS, Azure, local
- Same syntax as
!terraform.output- easy migration - Automatic in-memory caching for repeated calls
Basic Usage
The simplest form reads an output from another component in the same stack:
stacks/dev.yaml
Syntax
# Get output from component in the current stack
!terraform.state <component> <output>
# Get output from component in a different stack
!terraform.state <component> <stack> <output>
# Use YQ expressions for complex outputs
!terraform.state <component> <yq-expression>
!terraform.state <component> <stack> <yq-expression>
Cross-Stack References
Read outputs from components in different stacks by specifying the stack name:
stacks/prod-us-west-2.yaml
Dynamic Stack Names
Use template expressions to construct stack names dynamically:
# Reference the current stack
vpc_id: !terraform.state vpc {{ .stack }} vpc_id
# Construct stack name from context variables
vpc_id: !terraform.state vpc {{ printf "%s-%s-prod" .vars.tenant .vars.environment }} vpc_id
Working with Complex Outputs
Use YQ expressions to extract values from complex output types:
stacks/dev.yaml
Default Values
Provide default values for components that haven't been provisioned yet:
# String default
username: !terraform.state config ".username // ""default-user"""
# List default
subnet_ids: !terraform.state vpc ".private_subnet_ids // [""mock-subnet1"", ""mock-subnet2""]"
# Map default
config: !terraform.state settings '.config // {"env": "dev"}'
String Manipulation
Use YQ pipes to transform output values:
# Prepend and append strings
postgres_url: !terraform.state aurora-postgres ".hostname | ""jdbc:postgresql://"" + . + "":5432/db"""
Why !terraform.state is Faster
Execution Flow Comparison
!terraform.state (Fast Path):
- Resolve component context
- Read state file directly from backend
- Extract output value
!terraform.output (Slow Path):
- Resolve component context
- Generate backend configuration
- Generate variable files
- Initialize Terraform (download providers)
- Run
terraform outputcommand - Parse output
The direct backend access makes !terraform.state 10-100x faster depending on your setup.
Supported Backends
The !terraform.state function supports these backend types:
| Backend | Status |
|---|---|
s3 | Supported |
gcs | Supported |
azurerm | Supported |
local | Supported |
For other backends, use !terraform.output or !store.
Caching
Atmos automatically caches the results of !terraform.state calls in memory during command execution. If you reference the same component output multiple times, only the first call reads from the backend.
components:
terraform:
app:
vars:
# These all use the cached result after the first call
vpc_id: !terraform.state vpc vpc_id
vpc_id_again: !terraform.state vpc vpc_id
vpc_cidr: !terraform.state vpc cidr_block
Example: Multi-Tier Architecture
stacks/prod.yaml
Static Backend for Brownfield
For brownfield scenarios where you need to inject static values without a real Terraform state:
stacks/legacy.yaml
Considerations
- Secrets exposure: Using
!terraform.statewith sensitive outputs can expose data in commands likeatmos describe component - Permissions: You need read access to the state backend for all referenced components
- Cold starts: If the source component hasn't been provisioned, the function returns
nullunless you specify a default value - Cross-region DR: Be mindful of disaster recovery implications when reading state across regions
Migration from !terraform.output
Migrating from !terraform.output is straightforward - the syntax is identical:
# Before
vpc_id: !terraform.output vpc vpc_id
# After
vpc_id: !terraform.state vpc vpc_id
Simply replace !terraform.output with !terraform.state to get the performance benefits.