Terraform
publish_output block reference
Use the publish_output block to export values from your Stack to another Stack in the same project.
Background
Note
The publish_output block requires Terraform version 1.10.0-alpha20241009 or higher.
The publish_output block specifies a value to export from your current Stack, which other Stacks in the same project can consume using upstream_input blocks. Declare a publish_output block for each value you want to export from your Stack deployments. If the output value of a Stack changes after a run, HCP Terraform automatically triggers runs for any Stacks that depend on those outputs.
To learn more and review examples, refer to Pass data between Stacks.
Configuration model
The publish_output block supports the following arguments:
- publish_output "<LABEL>"block- valueexpression
- descriptionstring
 
Complete configuration
All available arguments are defined in the following publish_output block:
publish_output "<LABEL>" {
  description = "<DESCRIPTION>"
  value       = <VALUE_EXPRESSION>
}
Specification
A publish_output block supports the following configuration.
publish_output "<LABEL>"
The label after the publish_output keyword is a name for the published output, which must be unique among all published outputs in the same deployment configuration file. The name can be any valid identifier.
The following arguments are supported in a publish_output block:
| Argument | Description | Type | Required? | 
|---|---|---|---|
| value | The value to export from your Stack. | Expression | Required | 
| description | A human-friendly description for the output. | String | Optional | 
value
You must include a value argument in each publish_output block. Terraform evaluates the value argument's expression and exposes the result as the return value of an output and stores that value in state.
publish_output "vpc_id" {
  value = <VALUE>
}
Any valid expression can be an output value. Refer to Expressions to learn more.
Summary
- Data type: Expression
- Default: None
- Required: Yes
description
The description argument provides a human-friendly description for the published output. A description helps other Stack maintainers understand the purpose and content of the exported value.
publish_output "<LABEL>" {
  description = "<DESCRIPTION>"
  value       = <VALUE_EXPRESSION>
}
Summary
- Data type: String
- Default: None
Examples
The following examples demonstrate common use cases for publish_output blocks.
Fundamental publish an output
In the following example, a component configuration outputs the vpc_id that the web_server component creates whenever it is deployed:
main.tfcomponent.hcl
output "vpc_id" {
  description = "The ID of the main VPC for this environment"
  type        = string
  value       = component.web_server.vpc_id
}
component "web_server" {
  source = "./modules/web_server"
  inputs = {
    #...
  }
}
In your deployment configuration, the publish_output block exposes the value of the vpc_id output on the staging deployment using the publish_output block:
main.tfdeploy.hcl
publish_output "staging_vpc_id" {
  description = "The ID of the main VPC for this environment"
  value       = deployment.staging.vpc_id
}
deployment "staging" {
  # ...
}
Another Stack in the same project can now use the upstream_input block to consume the staging_vpc_id output from this Stack. Whenever the value of staging_vpc_id changes, HCP Terraform automatically triggers runs for Stacks that use staging_vpc_id.
Published output from multiple deployments
In the following example, two deployments, primary and secondary, publish their VPC IDs through separate published outputs primary_vpc_id and secondary_vpc_id:
deployment "primary" {
  inputs = {
    region = "us-west-2"
  }
}
deployment "secondary" {
  inputs = {
    region = "us-east-1"
  }
}
publish_output "primary_vpc_id" {
  description = "VPC ID from the primary region deployment"
  value       = deployment.primary.vpc_id
}
publish_output "secondary_vpc_id" {
  description = "VPC ID from the secondary region deployment"
  value       = deployment.secondary.vpc_id
}