Accessing Output Variables from Terraform and OpenTofu
As from v0.9 it is possible to obtain access to output variables from Terraform. As form 2.0 this is also possible for OpenTofu.
It is meant for sharing outputs of the infrastructure state with another part of the build.
Using it will cause the appropriate apply
and output
commands to run to ensure the state is up to date.
Do not use this to share state between two source sets.
Use terraform_remote_state data resource instead.
|
In order to obtain these variables, the JSON-output from the output
command is parsed.
The output variables are effectively a map of the parsed JSON and can be accessed via the following methods on a source set.
THe rawOutputVariables
methods return a provider to a map of all the output variables.
The rawOutputVariable
methods return a provider to a specific variable from the output variables.
You are responsible for traversing the values in the object and knowing its real type.
Only resolve these providers in the execution phase of the build. Resolving them too early will result in a build failure.
Provider<Map<String,Object>> myValues = terraform.sourceSets.main.rawOutputVariables('my-custom-workspace') (1) (2)
Provider<Object> cfId = terraform.sourceSets.main.rawOutputVariable('cloudfront_id', 'my-custom-workspace') (3)
1 | Both terraform.SourceSets and opentofu.sourceSets are supported. |
2 | Extract all the output variables from the main source set that are realted to the my-custom-workspace workspace. |
3 | Extract the variable called cloudfront_id from the my-custom-workspace workspace on the main source set. |