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
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:truehides the value in plan and apply. Optional.
See Output Values (opens in a new tab) in the OpenTofu docs.
Prerequisites
- OpenTofu installed. Installing OpenTofu (opens in a new tab) in the OpenTofu docs covers every system.
Step 1: Write a Resource
In an empty folder, create main.tf. It writes a file:
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:
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:
tofu init
tofu apply
# 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:
tofu output
file_id = "7acbf857459ea5fe9739305da206b2e6e1d84409"
file_name = "greeting.txt"
Clean Up
Run tofu destroy and type yes, then delete the folder.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting a value from a plan | A value that depends on a new resource shows (known after apply). | Apply, then read it with tofu output. |
Outputting a secret without sensitive = true | OpenTofu 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.
Related
- OpenTofu Tutorial — Part 4: Variables, tfvars and Outputs
- How to Use tofu output -raw and -json in OpenTofu
- How to Use Input Variables in OpenTofu
Additional Resources
- Output Values (opens in a new tab)
- Command: output (opens in a new tab)
- local_file resource (opens in a new tab)
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