An output block declares a value that a module explicitly returns after Terraform operations. Outputs connect independently managed infrastructure: a VPC module can return subnets, while an instance module can return an IP address or machine ID for another resource to use. section 3.4
Define an output with a single label and the value or behavior Terraform should expose:
- value specifies the returned expression, commonly a resource attribute; outputs can also return any value local to the module. section 3.4.1
- description documents the output for external tools, while sensitive tells Terraform not to log or print it. section 3.4.1
- depends_on explicitly creates a dependency between the output and another object; sensitive values still remain in state files. section 3.4.1
- Access a child module's outputs as attributes of its module block, then use those attributes in other resources. section 3.4.1
Pass an instance IP into a security rule
A reusable instance module can define an aws_instance_ip output from aws_instance.hello_world.private_ip. A caller then references module.my_instance.aws_instance_ip and appends /32 to restrict an HTTPS ingress rule to that instance's address. section 3.4.1
Listing 3.7 Adding outputs.tf to our module
output "aws_instance_arn" {
description = "The AWS Resource Name for the instance." #1
value = aws_instance.hello_world.arn #2
}
output "aws_instance_ip" {
description = "The IP Address for the private network interface on the
instance."
value = aws_instance.hello_world.private_ip
}
output "aws_instance" {
description = "The entire instance resource."
value = aws_instance.hello_world
}
#1 Like inputs, outputs can also have descriptions.
#2 Outputs can take in any value local to the module, including resource attributes.
Inspect a complete outputs.tf example showing descriptions, resource-derived values, and an output containing an entire resource. section 3.4.1
