Output Values
Imagine you’ve built a Lego city with Terraform. You know the houses, roads, and parks are there, but how do you tell your friends where the hospital is or what the address of the school is? In Terraform, that’s the job of output values.
They’re like signposts or reports that share important information about the resources you’ve created - IP addresses, URLs, IDs - so you can use them in other modules, scripts, or simply to verify what’s been deployed.
What are Output Values?
- Outputs are values returned by Terraform after applying a configuration.
- They expose resource attributes (like IP addresses, DNS names, IDs).
- Think of them as Terraform’s way of reporting back what it built.
Declaring Outputs
Outputs are declared in an outputs.tf file or directly in your configuration:
output "instance_ip" {
description = "Public IP of the EC2 instance"
value = aws_instance.example.public_ip
}
Using Outputs
- After running
terraform apply, Terraform prints the output values. - You can reference outputs in other modules or scripts.
- Example: Passing a database endpoint from one module to another.
Practical Use Cases
- Debugging: Quickly check resource attributes.
- Integration: Share values with other tools (CI/CD pipelines, Ansible).
- Chaining Modules: Pass outputs from one module as inputs to another.
- Documentation: Outputs act as a summary of deployed infra.
Hands‑On Lab / Demo
Lab: Displaying EC2 Instance IP
- Run:
terraform apply→ provisions EC2 instance.- Terraform prints the public IP in the output section.
Use the IP to SSH into the instance:
ssh ec2-user@<instance_ip>
Create outputs.tf:
output "instance_ip" {
description = "Public IP of the EC2 instance"
value = aws_instance.example.public_ip
}
Pro Tips & Best Practices
- Keep outputs organized in a separate
outputs.tffile. - Use descriptive names and add
descriptionfor clarity. - Only expose necessary attributes - avoid leaking sensitive data.
- Use outputs to simplify collaboration between modules.
- Outputs can be consumed programmatically via
terraform output -json.
Summary & Cheatsheet
- Outputs = Terraform’s report card.
- Declare in
outputs.tf. - Use cases: Debugging, integration, chaining modules, documentation.
- Command:
terraform outputto view values.
Quick mnemonic: Outputs = Share, Debug, Integrate
The Hackers Notebook
Output values make Terraform communicative and collaborative. They ensure that the results of your infrastructure aren’t hidden inside state files but are shared clearly with you, your team, and other tools. By mastering outputs, you’ve learned how to connect Terraform with the outside world.
