Terraform Output Variables

When building infrastructure with Terraform, you often need to expose certain values—like IP addresses, resource IDs, or connection strings—after deployment. That’s where output variables come in. Think of them as Terraform way of saying, “Here’s what I built, and here’s how you can use it.”
📘 What Are Output Variables?
Output variables allow you to extract and display information from your Terraform state. They’re especially useful when:
You want to pass values to another module or script.
You need to verify resource attributes post-deployment.
You're integrating Terraform with CI/CD pipelines or automation tools.
🛠️ Basic Syntax
Here’s the simplest form of an output variable:
output "instance_ip" {
value = aws_instance.web.public_ip
}
This will print the public IP of an EC2 instance named web
after terraform apply
.
🔍 Key Attributes
value
: The actual data you want to expose.description
: (Optional) A human-readable explanation.sensitive
: (Optional) Set totrue
to hide the value from CLI output (e.g., passwords).
output "db_password" {
value = aws_db_instance.db.password
sensitive = true
description = "The database password (hidden for security)"
}
📦 Output in Action
After running terraform apply
, you’ll see:
Outputs:
instance_ip = "54.210.123.45"
To retrieve outputs programmatically:
terraform output instance_ip
Or in JSON format:
terraform output -json
🔗 Passing Outputs Between Modules
Outputs can be consumed by other modules or scripts. For example:
module "network" {
source = "./network"
}
output "subnet_id" {
value = module.network.subnet_id
}
This makes modular design clean and maintainable.
🧠 Pro Tips for Terraform Outputs
Use outputs to debug and validate infrastructure changes.
Keep sensitive outputs hidden unless absolutely necessary.
Document outputs with
description
for better readability.Combine with
terraform_remote_state
to share outputs across workspaces.
Subscribe to my newsletter
Read articles from Deepak Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
