Skip to main content
0 reactions

How to Use Sensitive Variables in OpenTofu

Card titled Sensitive Variables, showing an api_key field whose value reads (sensitive value) under a crossed-out eye, with the notes Hidden in plan and apply and Still saved in the state file.
How to Use Sensitive Variables in OpenTofu.

Marking an OpenTofu® variable as sensitive keeps its value out of what tofu plan and tofu apply print. It’s for passwords, keys and tokens, so they don’t end up in a log or a screenshot.

This guide marks a variable as sensitive, shows what OpenTofu hides, and where the value still appears.

Syntax

SyntaxHCL
variable "<NAME>" {
  type      = <TYPE>
  sensitive = true
}

output "<NAME>" {
  value     = <EXPRESSION>
  sensitive = true
}
  • sensitive = true in a variable block hides its value, and every value built from it, in plan and apply output.
  • sensitive = true in an output block is required when the output uses a sensitive value.

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

Prerequisites

Step 1: Declare a Sensitive Variable

In an empty folder, create variables.tf:

variables.tfHCL
variable "api_key" {
  description = "Key for the payment service."
  type        = string
  sensitive   = true
}

Create main.tf. It writes the key into a settings file:

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

resource "local_file" "settings" {
  filename = "settings.txt"
  content  = "payment_api_key=${var.api_key}"
}

Create terraform.tfvars with a made-up key for this test:

terraform.tfvarsHCL
api_key = "demo-key-1234"

Step 2: Try to Output It

Create outputs.tf with an output that isn’t marked sensitive:

outputs.tfHCL
output "api_key" {
  value = var.api_key
}
TerminalBash
tofu init
tofu plan
OutputPlain text
│ Error: Output refers to sensitive values
│ 
│   on outputs.tf line 1:
│    1: output "api_key" {
│ 
│ To reduce the risk of accidentally exporting sensitive data that was intended to be
│ only internal, OpenTofu requires that any root module output containing sensitive data
│ be explicitly marked as sensitive, to confirm your intent.
│ 
│ If you do intend to export this data, annotate the output value as sensitive by adding
│ the following argument:
│     sensitive = true
╵

OpenTofu refuses, so a secret can’t leave the configuration by accident. Change outputs.tf as the message says:

outputs.tfHCL
output "api_key" {
  value     = var.api_key
  sensitive = true
}

Step 3: Apply and See What’s Hidden

Apply and type yes:

TerminalBash
tofu apply
OutputPlain text
  # local_file.settings will be created
  + resource "local_file" "settings" {
      + content              = (sensitive value)
      + 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             = "settings.txt"
      + id                   = (known after apply)
    }

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

Changes to Outputs:
  + api_key = (sensitive value)

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.settings: Creating...
local_file.settings: Creation complete after 0s [id=ed319299ff3613ce4bf1ef936d399bf5dd3a6f71]

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

Outputs:

api_key = <sensitive>

The file’s content and the output both show (sensitive value), because both come from var.api_key.

Step 4: Know Where the Value Still Appears

tofu output hides it too:

TerminalBash
tofu output
OutputPlain text
api_key = <sensitive>

But -raw prints it, on purpose, for scripts that need it:

TerminalBash
tofu output -raw api_key
OutputPlain text
demo-key-1234

Warning: Sensitive only hides values on screen. OpenTofu still saves them in the state file in plain text, and resources still get the real value: here, settings.txt contains the key. Protect the state as you would the secret itself (Sensitive Data in State (opens in a new tab)).

Clean Up

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

Common Mistakes

MistakeWhat happensDo this instead
Treating sensitive as encryptionThe value is still in the state file and in the resources.Protect the state, and keep secret files out of Git.
Committing terraform.tfvars with a real keyAnyone who can read the repository has the key.Keep real secrets out of files that go into Git.

FAQ

Can a provider still reveal a sensitive value?

Yes. The docs warn that a provider’s error message, or a resource ID built from the value, can show it.

How do I keep a secret out of the state?

OpenTofu 1.11 added ephemeral variables (opens in a new tab), which are never saved in the state, for the places that accept them. A later part of the OpenTofu Tutorial covers them.

Conclusion

Mark secrets as sensitive to keep them off the screen, mark outputs that use them too, and remember that the state still holds them.

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