Pass Data Between Terraform Modules with Outputs
Output values are how Terraform modules share resource data like IPs, ARNs, and IDs with each other.
What
Output values expose information from a Terraform module to the calling module or to the CLI. They are how you pass resource attributes like VPC IDs, IP addresses, and ARNs between modules. Outputs are also displayed after terraform apply and can be queried with terraform output.
Why It Matters
Without outputs, modules are black boxes that create resources but cannot share information. You need outputs to connect modules together, for example passing a VPC ID from a networking module to a compute module that launches instances in that VPC.
Example
# In modules/network/outputs.tf
output "vpc_id" {
description = "The ID of the VPC"
value = aws_vpc.main.id
}
output "db_password" {
description = "The database password"
value = random_password.db.result
sensitive = true # Hides value in CLI output
}
# In root module main.tf
module "network" {
source = "./modules/network"
}
resource "aws_instance" "web" {
subnet_id = module.network.vpc_id
# Reference outputs with module.<name>.<output>
}
# View outputs from CLI
terraform output # Show all outputs
terraform output vpc_id # Show specific output
terraform output -json # Machine-readable formatCommon Mistake
Not marking sensitive outputs with sensitive = true, causing secrets like database passwords and API keys to appear in plain text in CLI output, logs, and CI/CD pipeline artifacts.
Quick Fix
Add sensitive = true to any output that contains passwords, tokens, or other secrets. Note: sensitive = true only masks CLI output β the value is still stored in plaintext in the state file, so protect your state backend accordingly. Also add a description to every output so other engineers know what it provides and when to use it.
Key Takeaways
- 1Outputs expose module data to parent modules and CLI
- 2Reference with module.<name>.<output_name>
- 3Use sensitive = true for passwords and secrets
- 4terraform output shows all values after apply
- 5Always add description to document your outputs
Was this tip helpful?
Help us improve the DevOpsPath daily collection