Pass data from one Stack to another
If you have multiple Stacks that do not share a provisioning lifecycle, you can export data from one Stack for another Stack to consume. If the output value of a Stack changes after a run, HCP Terraform automatically triggers runs for any Stacks that depend on those outputs.
Background
You may need to pass data between different Stacks in your project. For example, one Stack in your organization may manage shared services, such as networking infrastructure, and another Stack may manage application components. Using separate Stacks lets you manage the infrastructure independently, but you may still need to share data from your networking Stack to your application Stack.
To output information from a Stack, declare a publish_output
block in the deployment configuration of the Stack exporting data. We refer to the Stack that declares a publish_output
block as the upstream Stack.
To consume the data exported by the upstream Stack, declare an upstream_input
block in the deployment configuration of a different Stack in the same project. We refer to the Stack that declares an upstream_input
block as the downstream Stack.
Requirements
The publish_output
and upstream_input
blocks require at least Terraform version terraform_1.10.0-alpha20241009
or higher. Download the latest version of Terraform to use the most up-to-date functionality.
Downstream Stacks must also reside in the same project as their upstream Stacks.
Declare outputs
You must declare a publish_output
block in your deployment configuration for each value you want to output from your current Stack.
For example, you can add a publish_output
block for the vpc_id
in your upstream Stack’s deployment configuration. You can directly reference a deployment's values with the deployment.deployment_name
syntax.
After applying your configuration, any Stack in the same project can now reference your network deployment's vpc_id
output by declaring an upstream_input
block.
Once you apply a Stack configuration version that includes your publish_output
block, HCP Terraform publishes a snapshot of those values, which allows HCP Terraform to resolve them. Meaning, you must apply your Stack’s deployment configuration before any downstream Stacks can reference your Stack's outputs.
Learn more about the publish_output
block.
Consume the output from an upstream Stack
Declare an upstream_input
block in your Stack’s deployment configuration to read values from another Stack's publish_output
block. Adding an upstream_input
block creates a dependency on the upstream Stack.
For example, if you want to use the output vpc_id
from an upstream Stack in the same project, declare an upstream_input
block in your deployment configuration.
After pushing your Stack's configuration into HCP Terraform, HCP Terraform searches for the most recently published snapshot of the upstream Stack your configuration references. If no snapshot exists, the downstream Stack's run fails.
If HCP Terraform finds a published snapshot for your referenced upstream Stack, then all of that Stack's outputs are available to this downstream Stack. Add upstream_input
blocks for every upstream Stack you want to reference. Learn more about the upstream_input
block.
Trigger runs when output values change
If an upstream Stack's published output values change, HCP Terraform automatically triggers runs for any downstream Stacks that rely on those outputs.
In the following example, the application
deployment depends on the upstream networking Stack.
The application Stack depends on the networking Stack’s output, so if the vpc_id
changes then HCP Terraform triggers a new run for the application Stack. This approach allows you to decouple Stacks that have separate life cycles and ensures that updates in an upstream Stack propagate to downstream Stacks.
Remove upstream Stack dependencies
To stop depending on an upstream Stack’s outputs, do the following in your downstream Stack's deployment configuration:
- Remove the upstream Stack's
upstream_input
block - Remove any references to the upstream Stack's outputs
- Push your configuration changes to HCP Terraform and apply the new configuration