liveBook · Manning

Terraform output

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

A Terraform output is a named configuration value exposed from a module for display or consumption by its caller.

Suppose a module creates an EC2 instance, and another resource needs its private IP address. An output makes that value available outside the module. Inputs pass values into a module; outputs expose values to its caller; locals remain inside it. Callers access an output as an attribute of the module block. section 3.2 section 3.8.3

Export and use an instance IP

In the module, define `output "aws_instance_ip" { value = aws_instance.hello_world.private_ip }`. A caller can then set a security-group ingress rule’s `cidr_ipv4` to `"${module.my_instance.aws_instance_ip}/32"`. The output passes the created instance’s address to the rule without requiring the caller to reach into the module’s resources. section 3.4.1

An `output` block’s `value` can refer to a resource attribute or another value local to the module. An optional `description` documents what callers receive. Outputs are commonly placed in `outputs.tf`; that filename is a convention, not a requirement for accessing them. section 3.4.1

Mark a password output `sensitive = true` to keep its value from being printed or logged by Terraform’s command-line interface. An output derived from a sensitive value must also be marked sensitive, or Terraform reports an error. This does not remove the value from Terraform state. section 3.4.2

Listing 6.5 Outputs in Terraform state
{
  "outputs": {                                              #1
    "password": {
      "value": "[-Cz\u003em@XQnZc",                         #2
      "type": "string",                                     #3
      "sensitive": true                                     #4
    }
  }
}

The state listing shows a root output with both its password value and `sensitive: true`: the flag does not erase the stored value. section 6.3.4

Terraform saves root-module outputs in state, allowing `terraform show` and `terraform_remote_state` to read them. Child-module outputs are not saved there as separate outputs; their values may instead be saved as attributes of resources that use them. An output already depends on its `value`. Add `depends_on` only when it must wait for something else—for example, a firewall rule—before becoming available. section 6.3.4 section 3.4.3

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

How deep it goes
Defines output arguments and caller access, explains state storage, and defers output preconditions.
Examples
Illustrates instance ARN, private IP, and whole-resource outputs, then uses the IP in a port-443 security-group rule.
What it assumes
Assumes familiarity with Terraform resources and modules while introducing the output block and its arguments.
What you can do after
Build it from scratch by exporting an instance attribute and using it in a security-group ingress rule.
How it connects
Connects module inputs and locals with resource attributes, dependencies, and Terraform state.

Also explained in

Compare how 2 books explain Terraform output →

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.