Implement !terraform.state Atmos YAML function#1363
Merged
Conversation
osterman
approved these changes
Jul 17, 2025
|
These changes were released in v1.183.0. |
This was referenced Jul 24, 2025
Merged
This was referenced Sep 3, 2025
This was referenced Oct 26, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
what
!terraform.stateAtmos YAML function!terraform.stateYAML Functionwhy
The
!terraform.stateYAML function allows reading the outputs (remote state) of components in Atmos stack manifests directly from the configured Terraform/OpenTofu backends.NOTE:
Currently, the
!terraform.stateYAML function supports the following backend types:local(Terraform and OpenTofu)s3(Terraform and OpenTofu)As support for new backend types is added (e.g.
azurerm,gcs), this document will be updated accordingly. Meanwhile, if you are using backends other thanlocalands3, consider the!storeor!terraform.outputYAML function to read remote state and share data between components.Usage
The
!terraform.statefunction can be called with either two or three parameters:NOTE:
You can use Atmos Stack Manifest Templating in the
!terraform.stateYAML function expressions. Atmos processes the templates first, and then executes the!terraform.statefunction, allowing you to provide the parameters to the function dynamically.!terraform.stateFunction Execution FlowWhen processing the
!terraform.stateYAML function for a component in a stack, Atmos executes the following steps:Stack and Component Context Resolution
Atmos resolves the full context for the specified component within the given stack, including all inherited and merged configuration layers (globals, environment and component-level config).
Terraform State Backend Lookup and Read
Based on the resolved context, Atmos identifies the corresponding backend for the component in the stack and reads the state file directly from the backend. If access to the backend requires a role assumption (e.g.
assume_role.role_arnfor thes3backend), Atmos assumes the role before accessing the backend state file.Output Parsing and Interpolation
The relevant output variable is extracted from the state file (using a YQ parser). Atmos parses and interpolates the value into the final configuration structure, replacing the
!terraform.statedirective in the YAML stack manifest with the final value.NOTE:
The
!terraform.statefunction accepts the same parameters and produces the same result as the!terraform.outputfunction, but has significantly less impact on performance as it reads the state file directly from the configured backend without executing Terraform/OpenTofu commands, generating varfiles and backend config files, and initializing all modules and providers. To understand the performance implications of the!terraform.outputand!terraform.statefunctions, compare the !terraform.output Execution Flow with the !terraform.state Execution Flow.Using YQ Expressions to retrieve items from complex output types
To retrieve items from complex output types such as maps and lists, or do any kind of filtering or querying, you can utilize YQ expressions.
For example:
For more details, review the following docs:
Using YQ Expressions to provide a default value
If the component for which you are reading the output has not been provisioned yet, the
!terraform.statefunction will return the string<no value>unless you specify a default value in the YQ expression, in which case the function will return the default value.This will allow you to mock outputs when executing
atmos terraform planwhere there are dependencies between components, and the dependent components are not provisioned yet.NOTE:
To provide a default value, you use the
//YQ operator.The whole YQ expression contains spaces, and to make it a single parameter, you need to double-quote it.
YQ requires the strings in the default values to be double-quoted as well.
This means that you have to escape the double-quotes in the default values by using two double-quotes.
For example:
Read the
usernameoutput from theconfigcomponent in the current stack.If the
configcomponent has not been provisioned yet, return the default valuedefault-userRead the
private_subnet_idsoutput from thevpccomponent in the current stack.If the
vpccomponent has not been provisioned yet, return the default value["mock-subnet1", "mock-subnet2"]Read the
config_mapoutput from theconfigcomponent in the current stack.If the
configcomponent has not been provisioned yet, return the default value{"api_endpoint": "localhost:3000", "user": "test"}For more details, review the following docs:
Specifying Atmos
stackIf you call the
!terraform.statefunction with three parameters, you need to specify the stack as the second argument.There are multiple ways you can specify the Atmos stack parameter in the
!terraform.statefunction.Hardcoded Stack Name
Use it if you want to get an output from a component from a different (well-known and static) stack. For example, you have a
tgwcomponent in a stackplat-ue2-devthat requires thevpc_idoutput from thevpccomponent from the stackplat-ue2-prod:Reference the Current Stack Name
Use the
.stack(or.atmos_stack) template identifier to specify the same stack as the current component is in(for which the
!terraform.statefunction is executed):For example, you have a
tgwcomponent that requires thevpc_idoutput from thevpccomponent in the same stack:Use a Format Function
Use the
printftemplate function to construct stack names using static strings and dynamic identifiers.This is convenient when you want to override some identifiers in the stack name:
For example, you have a
tgwcomponent deployed in the stackplat-ue2-dev. Thetgwcomponent requires thevpc_idoutput from thevpccomponent from the same environment (ue2) and same stage (dev), but from a different tenantnet(instead ofplat):Caching the result of
!terraform.statefunctionAtmos caches (in memory) the results of
!terraform.statefunction.The cache is per Atmos CLI command execution, e.g., each new execution of a command like
atmos terraform plan,atmos terraform applyoratmos describe componentwill create and use a new memory cache, which involves re-invokingterraform outputsafter reinitialization.If you define the function in stack manifests for the same component in a stack more than once, the first call will produce the result and cache it, and all the consecutive calls will just use the cached data. This is useful when you use the
!terraform.statefunction for the same component in a stack in multiple places in Atmos stack manifests. It will speed up the function execution and stack processing.For example:
In the example, the
test2Atmos component uses the outputs (remote state) of thetestAtmos component from the same stack. The YAML function!terraform.stateis executed three times (once for each tag).After the first execution, Atmos caches the result in memory, and reuses it in the next two calls to the function. The caching makes the stack processing much faster. In a production environment where many components are used, the speedup can be significant.
Using
!terraform.statewithstaticremote state backendAtmos supports brownfield configuration by using the remote state of type
static.For example:
When the functions are executed, Atmos detects that the
static-backendcomponent has thestaticremote state configured, and instead of executingterraform output, it just returns the static values from theremote_state_backend.staticsection.Executing the command
atmos describe component eks-cluster -s <stack>produces the following result:Considerations
Using
!terraform.statewith secrets can expose sensitive data to standard output (stdout) in any commands that describe stacks or components.When using
!terraform.statewithatmos describe affected, Atmos requires access to all referenced remote states.If you operate with limited permissions (e.g., scoped to
dev) and reference production stacks, the command will fail.Overusing the function within stacks to reference multiple components can impact performance.
Be mindful of disaster recovery (DR) implications when using it across regions.
Consider cold-start scenarios: if the dependent component has not yet been provisioned,
terraform outputwill fail.Summary by CodeRabbit
Summary by CodeRabbit
New Features
!terraform.stateYAML function for direct, efficient reading of Terraform/OpenTofu outputs from backend state files, supporting local and S3 backends.Improvements
!terraform.state.Bug Fixes
Tests
Chores