Outputs
How to declare and assign Terraform outputs
With an output, you can expose selected values from a configuration or a module - making them accessible after running terraform apply.
To do so, add an output block to your configuration. With outputs you can:
- Pass resource attributes from a child module up to its parent module.
- Print values to the CLI after a run.
- Share a root module's outputs with other Terraform configurations through the
terraform_remote_statedata source, if they use remote state (State Sharing). - Hand data from a Terraform run over to automation tools, such as Continuous Deployment.
Defining an Output
output "ip_address" {
description = "IP address of the server"
value = hcloud_server.web.ipv4_address
}
Outputs are considered crucial as a part of the automation ecosystem of corporations. Passing values from one stack to another is where Terraform fully shines.
Passing Values from a Child Module to its Parent
A parent module can't just reach into a child module and grab its values - the child has to expose them as outputs first. In the example below, the network module creates a network and exposes its ID:
# modules/network/outputs.tf
output "network_id" {
description = "ID of the private network"
value = hcloud_network.main.id
}
The parent module can then use the value with module.<name>.<output>:
# main.tf
module "network" {
source = "./modules/network"
}
resource "hcloud_server_network" "web" {
server_id = hcloud_server.web.id
network_id = module.network.network_id
}Printing Values to the CLI
When running terraform apply, the outputs of the root module are printed at the end:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
ip_address = "95.217.12.34"
The outputs can also be viewed afterwards - without having to run apply again:
terraform output # all outputs
terraform output ip_address # a single output
Sensitive values, such as passwords, can be hidden with sensitive = true. Keep in mind - they are still stored in plain text in the state file.
output "db_password" {
value = random_password.db.result
sensitive = true
}Sharing Outputs Between Configurations
If the state is stored remotely, other configurations can read the outputs of a root module through the terraform_remote_state data source. This makes it possible to split the infrastructure into separate stacks - e.g. one for networking and one for applications:
data "terraform_remote_state" "network" {
backend = "remote"
config = {
organization = "my-org"
workspaces = {
name = "network-prod"
}
}
}
resource "hcloud_server_network" "web" {
server_id = hcloud_server.web.id
network_id = data.terraform_remote_state.network.outputs.network_id
}
Only the outputs of the root module are accessible this way. Values from child modules have to be passed up to the root module first.
Passing Data to Automation Tools
Scripts and CI/CD pipelines can read outputs as well - making it possible to use values that only exist after Terraform has run, such as the IP address of a server. Use -raw for a single plain value, or -json for all outputs in a machine-readable format:
SERVER_IP=$(terraform output -raw ip_address)
ssh root@$SERVER_IP "docker compose up -d"
In GitHub Actions, the value can be passed on to the following steps:
- name: Get server IP
id: tf
run: echo "ip=$(terraform output -raw ip_address)" >> $GITHUB_OUTPUT
- name: Deploy
run: ssh root@${{ steps.tf.outputs.ip }} "docker compose up -d"