Skip to main content
0 reactions

How to Use Output Values in OpenTofu

Card titled Output Values in OpenTofu, showing an outputs.tf file with an output named file_name and the result file_name equals greeting.txt.
How to Use Output Values in OpenTofu.

An output value is something OpenTofu® prints at the end of tofu apply: a file name, an address, an ID. You declare it once, and it saves you from digging through the state to find it.

This guide adds two outputs to a small configuration and shows when their values appear.

Syntax

SyntaxHCL
output "<NAME>" {
  description = "<TEXT>"
  value       = <EXPRESSION>
  sensitive   = <BOOL>
}
  • <NAME>: the output’s name, printed with its value.
  • value: what to print. Any expression works, such as a resource’s attribute or a variable.
  • description: what the output holds. Optional.
  • sensitive: true hides the value in plan and apply. Optional.

See Output Values (opens in a new tab) in the OpenTofu docs.

Prerequisites

Step 1: Write a Resource

In an empty folder, create main.tf. It writes a file:

main.tfHCL
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.9"
    }
  }
}

resource "local_file" "greeting" {
  filename = "greeting.txt"
  content  = "Welcome to the Bookshop!"
}

Step 2: Add Outputs

Create outputs.tf:

outputs.tfHCL
output "file_name" {
  description = "Name of the file OpenTofu wrote."
  value       = local_file.greeting.filename
}

output "file_id" {
  description = "ID OpenTofu gave the file, worked out when it is created."
  value       = local_file.greeting.id
}

local_file.greeting.filename reads the file name from the resource. id is set only when OpenTofu creates the file.

Step 3: Apply

Download the local provider, then apply and type yes:

TerminalBash
tofu init
tofu apply
OutputPlain text
  # local_file.greeting will be created
  + resource "local_file" "greeting" {
      + content              = "Welcome to the Bookshop!"
      + content_base64sha256 = (known after apply)
      + content_base64sha512 = (known after apply)
      + content_md5          = (known after apply)
      + content_sha1         = (known after apply)
      + content_sha256       = (known after apply)
      + content_sha512       = (known after apply)
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "greeting.txt"
      + id                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + file_id   = (known after apply)
  + file_name = "greeting.txt"

Do you want to perform these actions?
  OpenTofu will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

local_file.greeting: Creating...
local_file.greeting: Creation complete after 0s [id=7acbf857459ea5fe9739305da206b2e6e1d84409]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

file_id = "7acbf857459ea5fe9739305da206b2e6e1d84409"
file_name = "greeting.txt"

In the plan, file_name already has its value, but file_id says (known after apply). Under Outputs, both have values, because the file now exists.

To print the outputs again later, run tofu output:

TerminalBash
tofu output
OutputPlain text
file_id = "7acbf857459ea5fe9739305da206b2e6e1d84409"
file_name = "greeting.txt"

Clean Up

Run tofu destroy and type yes, then delete the folder.

Common Mistakes

MistakeWhat happensDo this instead
Expecting a value from a planA value that depends on a new resource shows (known after apply).Apply, then read it with tofu output.
Outputting a secret without sensitive = trueOpenTofu stops with Output refers to sensitive values.Mark the output as sensitive.

FAQ

Where are output values stored?

In the state, next to the resources. That’s why tofu output can print them without planning.

Can another configuration read my outputs?

Yes: a module’s outputs are how its caller reads values from it, and the root module’s outputs can be read from its state by other configurations.

Conclusion

Add an output block for every value you look up after an apply, and give it a description so others know what it is.

Additional Resources

OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.

0 comments