How Terraform output blocks expose and manage configuration values like IPs, DNS names, IDs and secrets, including examples, commands, sensitivity handling, and best practices.
Welcome to this lesson on the Terraform output block. Outputs let you expose important data produced by your Terraform runs—such as IP addresses, DNS names, resource IDs, and connection strings—so people, scripts, and other modules can consume them without digging through cloud consoles or re-running queries.
These resource blocks create an Azure Resource Group and a Virtual Network. After creation, surface values such as the resource group name, VNet ID, or subnet IDs with outputs so other systems or modules can use them.
Below are typical outputs you might declare for an EC2 instance and a load balancer. These show simple attribute exposure, composed values, and how to produce a ready-to-use URL.
# Output Instance IP Addressoutput "instance_public_ip" { description = "Public IP of Server" value = aws_instance.web.public_ip}# Output DNS Name for Load Balanceroutput "website_dns" { description = "Website DNS Record" value = aws_elb.web_app.dns_name}# Output Friendly URL of Websiteoutput "website_url" { description = "Friendly URL for the website" value = "https://${aws_alb.web.dns_name}"}
instance_public_ip: exposes a single resource attribute.
website_dns: exports the load balancer’s DNS name.
website_url: composes a click-ready HTTPS URL around a resource attribute.
Mark outputs as sensitive = true for any secret or credential. Sensitive outputs are still recorded in the Terraform state (so secure your backend accordingly). Treat your state file as sensitive data.
Remember outputs are stored in state: protect your state backend and access control.
Limit outputs to values that are useful externally (URLs, IPs, IDs required by other systems).
Prefer secret managers for long-term secret storage; do not rely solely on outputs for sensitive secrets.
Use outputs to pass data between modules: a child module declares outputs that the parent (or other modules) can read.
Avoid exposing low-value or noisy attributes that are not consumed by other systems.
Do not store highly sensitive secrets only in outputs unless your state backend is secured and access is tightly controlled. Consider using dedicated secret management solutions for production secrets.