liveBook · Manning

Output block

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

An output block is a Terraform configuration block that declares a value exposed after Terraform operations.

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

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains labels, arguments, references, dependencies, and state handling, while deferring preconditions to a later treatment.
Examples
Uses subnet, IP address, machine ID, ARN, private IP, whole-resource, and security-group ingress examples.
What it assumes
Assumes familiarity with Terraform blocks and resource attributes while introducing module value flow through concrete configurations.
What you can do after
Builds module outputs and applies their attributes in downstream Terraform resources.
How it connects
Ties output blocks to modules, resources, variables, data blocks, dependencies, and state files.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.