TerraformPilot

Terraform

Terraform Outputs Explained - Share Data Between Modules

Master Terraform outputs for sharing data between modules and stacks. Covers output types, sensitive outputs, remote state data sources, and cross-stack...

LLuca Berton1 min read

Quick Answer

#

Outputs expose values from your Terraform configuration — print them in the terminal, pass them between modules, or share them across projects via remote state data sources. Mark sensitive outputs with sensitive = true.

Basic Outputs

#
output "vpc_id" {
  value       = aws_vpc.main.id
  description = "ID of the VPC"
}
 
output "public_ip" {
  value       = aws_instance.web.public_ip
  description = "Public IP of the web server"
}
 
output "db_endpoint" {
  value       = aws_db_instance.main.endpoint
  description = "RDS endpoint"
  sensitive   = true
}
# View outputs
terraform output
terraform output vpc_id
terraform output -json
terraform output -raw public_ip  # No quotes, useful in scripts

Module Outputs

#

Child Module (modules/networking/outputs.tf)

#
output "vpc_id" {
  value = aws_vpc.main.id
}
 
output "private_subnet_ids" {
  value = aws_subnet.private[*].id
}
 
output "public_subnet_ids" {
  value = aws_subnet.public[*].id
}

Root Module (main.tf)

#
module "networking" {
  source = "./modules/networking"
  vpc_cidr = "10.0.0.0/16"
}
 
module "compute" {
  source     = "./modules/compute"
  vpc_id     = module.networking.vpc_id            # Use module output
  subnet_ids = module.networking.private_subnet_ids # Use module output
}
 
# Expose module outputs at root level
output "vpc_id" {
  value = module.networking.vpc_id
}

Cross-Project State Sharing

#
# Project B reads outputs from Project A's state
data "terraform_remote_state" "networking" {
  backend = "s3"
  config = {
    bucket = "my-terraform-state"
    key    = "prod/networking/terraform.tfstate"
    region = "us-east-1"
  }
}
 
resource "aws_instance" "web" {
  subnet_id = data.terraform_remote_state.networking.outputs.private_subnet_ids[0]
}

Sensitive Outputs

#
output "db_password" {
  value     = random_password.db.result
  sensitive = true  # Hidden in terminal, visible in state file
}
 
# Access sensitive outputs explicitly
# terraform output -json db_password
# terraform output -raw db_password

Output Types

#
# String
output "region" { value = var.region }
 
# List
output "subnet_ids" { value = aws_subnet.private[*].id }
 
# Map
output "endpoints" {
  value = {
    web = aws_instance.web.public_ip
    api = aws_instance.api.private_ip
    db  = aws_db_instance.main.endpoint
  }
}
 
# Computed
output "connection_string" {
  value     = "postgresql://${var.db_user}:${random_password.db.result}@${aws_db_instance.main.endpoint}/mydb"
  sensitive = true
}

Best Practices

#
  • Add description to every output — documents what the value is for
  • Mark secrets as sensitive — prevents accidental logging
  • Output IDs, not full resources — keep interfaces clean
  • Use consistent namingvpc_id not the_vpc_identifier
  • Expose only what's needed — don't output everything
#

Conclusion

#

Outputs are how Terraform modules communicate. Use them to share IDs, endpoints, and config between modules, expose important values in the terminal, and connect projects via remote state. Always add descriptions and mark sensitive values.

#Terraform#Infrastructure as Code#Best Practices

Share this article