Terraform Output Variables: Syntax, Examples & Best Practices
Output variables in Terraform help expose important information from your infrastructure after deployment.
Instead of searching through resource attributes or manually inspecting cloud resources, you can display only the values that matter. This makes Terraform configurations easier to use, automate, and maintain.
Whether you're deploying your first AWS resource or managing a large multi-module platform, output variables help teams retrieve deployment results quickly and consistently.
What Are Output Variables in Terraform and Why Do They Matter?
Output variables in Terraform are used to expose selected values from your infrastructure after Terraform creates or updates resources.
These values can come from Terraform resource attributes such as:
- Resource IDs
- Public IP addresses
- DNS names
- ARNs
- Database endpoints
- Load balancer URLs
For example, after creating an EC2 instance, you may want Terraform to display its public IP address automatically instead of searching for it in the AWS console.
Most output values are generated from existing resource attributes. If you're unfamiliar with how resource attributes work, see our guide on Terraform Resource Attributes.
Output variables work similarly to return values in programming languages. They allow Terraform configurations and modules to expose useful information for users, scripts, and other infrastructure components.
Why Use Output Variables in Terraform?
Output variables solve several practical problems in real-world Terraform deployments.
Return Critical Resource Information
After a deployment finishes, engineers often need specific details such as:
- Instance IDs
- Public IP addresses
- VPC IDs
- Load balancer DNS names
- Database endpoints
Output variables provide these values immediately without requiring additional lookups.
Improve Deployment Readability
Terraform deployments can create dozens or even hundreds of resources.
Instead of scrolling through logs or inspecting cloud consoles, outputs display the most relevant information directly after terraform apply.
Enable Communication Between Modules
When working with reusable Terraform modules, one module often needs information created by another.
Output variables allow modules to expose values safely and predictably.
Support Automation and CI/CD Pipelines
Many automation workflows rely on Terraform outputs.
Scripts, CI/CD pipelines, and deployment tools can retrieve output values and pass them into later stages without manual intervention.
How to Define Output Variables in Terraform
Terraform uses the output block to define output variables.
Most teams place output definitions in a dedicated outputs.tf file to keep configurations organized.
Basic Syntax
output "instance_ip" {
value = aws_instance.my_instance.public_ip
description = "Public IP address of the EC2 instance"
}
Understanding the Components
outputdefines the output block.valuespecifies the data Terraform should expose.descriptiondocuments the purpose of the output.
After Terraform applies the configuration successfully, the output value appears in the terminal.
Using Sensitive Outputs
Some values should not be displayed openly.
Terraform allows outputs to be marked as sensitive:
output "db_password" {
value = aws_db_instance.mydb.password
sensitive = true
}
When marked as sensitive, Terraform hides the value from standard CLI output.
Pro Tip: Marking an output as sensitive only hides it from normal terminal output. The value still exists in Terraform state. Protect your state backend appropriately using secure storage and access controls.
For a deeper understanding of state storage, see our guide on Terraform State.
Output Variables vs Input Variables in Terraform
Input variables and output variables serve different purposes.
Input variables allow users to provide values to Terraform configurations.
Output variables expose values generated by Terraform resources after deployment.
A common workflow looks like this:
- Input variables provide configuration values.
- Terraform creates infrastructure.
- Output variables expose deployment results.
If you're learning how variables work, read our guide on Input Variables in Terraform.
How to Access Output Variables After Deployment
Terraform automatically displays output values after a successful deployment.
You can also retrieve them later using the Terraform CLI.
Retrieve a Specific Output
terraform output instance_ip
Example output:
34.201.10.25
Retrieve All Outputs
terraform output
Retrieve Outputs in JSON Format
terraform output -json
JSON output is useful for automation scripts and integrations.
Retrieve Raw Values
terraform output -raw load_balancer_dns
This option is commonly used in CI/CD pipelines where scripts need a clean value without additional formatting.
Using Output Variables with Terraform Modules
Output variables become especially valuable in modular Terraform architectures.
Consider a networking module that creates a VPC and exposes its ID:
# modules/network/outputs.tf
output "vpc_id" {
value = aws_vpc.main.id
}
A root module can then access the output:
module "network" {
source = "./modules/network"
}
output "vpc_id" {
value = module.network.vpc_id
}
This pattern allows infrastructure components to communicate without tightly coupling their configurations.
If you're building reusable infrastructure, check out our guides on:
Common Real-World Use Cases for Terraform Output Variables
Networking Deployments
Output variables are frequently used to expose:
- VPC IDs
- Subnet IDs
- Route table IDs
- Security group IDs
- Load balancer DNS names
These values are often consumed by other modules or environments.
Infrastructure Pipelines
CI/CD systems frequently retrieve outputs after deployment.
Examples include:
- Updating DNS records
- Running integration tests
- Configuring monitoring systems
- Triggering post-deployment validation
Multi-Environment Deployments
Teams managing development, staging, and production environments often use outputs to expose environment-specific information consistently.
Cloud Resource Provisioning
For example, after deploying AWS resources with Terraform:
- EC2 instance IDs can be exposed.
- S3 bucket names can be returned.
- RDS endpoints can be displayed.
- Load balancer URLs can be published.
Related guides:
Best Practices for Using Output Variables in Terraform
Keep Outputs Organized
Store output definitions in an outputs.tf file whenever possible.
This keeps infrastructure code easier to navigate.
Use Descriptive Names
Avoid generic names like:
output "id"
Prefer descriptive names:
output "web_server_public_ip"
Expose Only Necessary Values
Not every resource attribute needs to become an output.
Expose only the information that users, modules, or automation workflows genuinely require.
Protect Sensitive Information
Use:
sensitive = true
for:
- Passwords
- Authentication tokens
- API keys
- Private credentials
Design Stable Module Interfaces
When building modules, treat outputs as part of the module's public interface.
Frequent output changes can break downstream consumers.
Common Mistakes to Avoid
Exposing Too Many Outputs
Large output lists make modules difficult to maintain and understand.
Only expose meaningful values.
Exposing Secrets Unnecessarily
Sensitive information should be output only when absolutely required.
Even sensitive outputs remain stored in Terraform state.
Using Ambiguous Names
Names such as:
output "value"
provide little context.
Choose names that clearly describe the data being returned.
Forgetting Module Outputs
A resource inside a module cannot be referenced directly from outside the module.
The module must explicitly expose the value through an output block.
Official Documentation and References
For the latest Terraform behavior and syntax, refer to:
Frequently Asked Questions (FAQs)
What are output variables in Terraform?
Output variables expose selected values from Terraform-managed infrastructure after deployment.
How do I define an output variable?
Use the output block and specify a value:
output "instance_ip" {
value = aws_instance.web.public_ip
}
Can I hide sensitive output values?
Yes.
Use:
sensitive = true
Terraform will suppress the value from standard CLI output, although it remains stored in Terraform state.
Are output variables required?
No.
Terraform works without outputs, but they improve usability, automation, and module design significantly.
Can output variables be used across modules?
Yes.
Outputs are the standard way for one Terraform module to expose values that other modules can consume.
Where are Terraform outputs stored?
Terraform stores outputs in the state file. This is why securing state storage is important, especially when outputs contain sensitive information.
Final Thoughts
Output variables are one of the simplest features in Terraform, but they have a significant impact on usability and maintainability.
They help teams expose important deployment results, build reusable modules, simplify automation workflows, and reduce manual lookups.
As your infrastructure grows, well-designed output variables become an important part of creating clean, scalable, and production-ready Terraform configurations.
8 free, 100% client-side tools for developers — no signup, no data uploads.
Explore all tools