Terraform Outputs
What are Terraform Outputs?
Outputs are a way to expose specific values from your Terraform configuration. They can be used to display important information after applying your configuration or to pass data between modules.
Defining Outputs
Outputs are typically defined in an outputs.tf
file:
output "instance_ip_addr" {
value = aws_instance.server.private_ip
description = "The private IP address of the main server instance."
}
Output Syntax
An output block consists of:
- The output
keyword
- A unique name
- A value
argument specifying the data to be output
- An optional description
- Optional sensitive
argument to hide the output value
Using Outputs
Outputs are displayed after running terraform apply
:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
instance_ip_addr = "10.0.1.4"
You can also query outputs using terraform output
:
$ terraform output instance_ip_addr
10.0.1.4
Sensitive Outputs
For sensitive data, use the sensitive
argument:
output "db_password" {
value = aws_db_instance.db.password
description = "The database password"
sensitive = true
}
Sensitive outputs are hidden in the CLI output but still stored in the state file.
Outputs in Modules
Outputs are crucial for modules. They allow a child module to pass data back to its parent:
# In a child module
output "instance_ip" {
value = aws_instance.example.private_ip
}
# In the parent module
module "web_server" {
source = "./web_server"
}
output "web_server_ip" {
value = module.web_server.instance_ip
}
Remember that outputs are stored in the Terraform state file, so avoid putting sensitive information in outputs unless absolutely necessary.