The pipeline finished green, the new Postgres instance was up, and the only thing the application team needed was the connection password. The engineer ran terraform apply again, scrolled to the outputs and found db_password = <sensitive>. Two minutes later there was a pull request that deleted sensitive = true from the output block, with the description "unblock app team".

That pull request is the anti-pattern this guide is about. Terraform hides the value on purpose, and the value is still available in at least three places without removing the protection. Deleting the flag works, in the sense that the password appears. It also prints it into every plan and apply log from then on, including the CI logs that dozens of people can open.

Why deleting the sensitive flag is the wrong fix

Start with why the flag exists, because the fix depends on it. Terraform's command output is meant to be safe to read over someone's shoulder and safe to store in a CI system. Plans and applies run on shared runners, logs get attached to tickets, and job output stays in tools such as GitHub Actions for 90 days by default. A secret printed there once has to be treated as exposed.

When an output is marked sensitive = true, Terraform replaces its value with <sensitive> in the plan, in the apply summary and in the bare terraform output listing. When a value comes from something sensitive, such as an input variable declared sensitive or a provider attribute like the password of an aws_db_instance or the result of a random_password, Terraform tracks that mark through every expression that uses it.

Since version 0.15, if you try to expose such a value through an output without declaring the output sensitive, Terraform stops with the error "Output refers to sensitive values" and asks you to add the flag.

So there are really two situations behind the symptom. In the first, you declared the output sensitive yourself and now want to see it. In the second, the mark came from upstream, and the output was forced to be sensitive. The tempting fixes differ: in the first case people delete the flag, in the second they wrap the expression in nonsensitive(). Both have the same effect on logs, and both are almost always the wrong answer for a real secret.

There is a legitimate use for nonsensitive(). Providers sometimes mark values that are not secret at all, for example an ID derived from a sensitive attribute, or a hash computed from a password. Unwrapping those is fine and makes plans readable again. The test is simple: would you paste this exact value into a public chat? If not, leave it marked.

What Terraform actually redacts, and where the value still lives

The redaction is narrower than many teams assume. It applies to what Terraform prints in human-oriented views, not to what it stores or what it returns on direct request.

Sensitive output: an output block with sensitive = true. Terraform redacts it in plan and apply output and when listing all outputs, but returns the real value when you query that output by name or request JSON.

nonsensitive(): a function, available since Terraform 0.15, that removes the sensitive mark from a value. It changes display behaviour only; the value was never encrypted.

Ephemeral value: a value that Terraform uses during a run but never writes to the plan file or the state. Ephemeral input variables, ephemeral outputs in child modules and ephemeral resources arrived in Terraform 1.10, released in late 2024.

Write-only argument: a resource argument, added in Terraform 1.11, that accepts an ephemeral value and sends it to the provider without persisting it, such as a database password argument whose name ends in _wo in providers that support it.

The important consequence: a sensitive value is stored in plain text in the state file. Anyone who can run terraform state pull, read the S3 bucket or Azure storage container holding the state, or open the state version in HCP Terraform can read every secret it contains. terraform show -json prints the full state, sensitive values included, with a separate sensitive_values structure that only tells consumers which fields were marked.

The CLI behaviour, as HashiCorp documents it, is that Terraform does not redact a sensitive output when you query it by name, when you query all outputs in JSON format, or when a root module uses outputs from a child module. That is the intended route for a human or a script that genuinely needs the value. In practice:

terraform output                    # db_password = <sensitive>
terraform output db_password        # "s3cr3t-value" (quoted)
terraform output -raw db_password   # s3cr3t-value (no quotes, no newline)
terraform output -json              # every output, plain text, as JSON

Remote runs add one more layer. In HCP Terraform and Terraform Enterprise, the workspace page shows sensitive outputs as hidden, and the tfe_outputs data source in the tfe provider returns every output of another workspace marked as sensitive, with a separate attribute for the values that were not flagged. That is convenient for wiring workspaces together and easy to misuse: a downstream workspace that unwraps those values with nonsensitive() prints them in its own plan logs, two hops away from the place where the secret was created.

The terraform console command keeps the mark and shows (sensitive value), unless you call nonsensitive() inside the console session, which affects only that session and nothing in the code.

The ways to read it without unmasking everything

The right method depends on who needs the value and how often. These questions narrow it down:

  1. Does a person need the value once, for example to test a connection? If yes, run terraform output -raw NAME locally, with your own credentials, and do not paste it into tickets or chat. If no, go to 2.
  2. Does another Terraform configuration need it? If yes, read it from a secrets manager in that configuration, or as a last resort through terraform_remote_state, which exposes all outputs of the source state to anyone with access. If no, go to 3.
  3. Does a CI job or script need it? If yes, read it with terraform output -raw into a variable, mask it in the CI system before any echo, and never pass it as a command-line argument that shows in process listings. If no, go to 4.
  4. Does an application need it at runtime? If yes, the value should not come from Terraform outputs at all. Store it in AWS Secrets Manager, HashiCorp Vault, Azure Key Vault or Google Secret Manager, and have the application read it there. If no, nobody needs the value, and it should stay marked.

For the CI case, a safe procedure on GitHub Actions looks like this:

  1. Run terraform init and make sure the job has read access to the state backend and nothing broader.
  2. Capture the value with terraform output -raw db_password into a shell variable, without printing it.
  3. Immediately register it as a secret mask with the ::add-mask:: workflow command so any accidental later print is replaced with asterisks.
  4. Pass it to the next step through a masked step output or an environment variable, not through a file committed to the workspace.
  5. Avoid set -x and verbose flags in the same step, since they print expanded variables before the mask can help.
  6. Remove the variable at the end of the step, and review the job log once by hand after the first run.

For the application case, many resources can now create the secret directly in a manager. On AWS, setting manage_master_user_password = true on an RDS instance makes RDS generate the master password and keep it in Secrets Manager, so Terraform never sees it and no output is needed. That is a better shape than any output trick, because it removes the secret from state entirely.

An illustrative leak, and the move toward never storing the secret

Here is an illustrative case, composed from patterns that are common in infrastructure teams rather than taken from one company. A platform team of five managed about 40 Terraform workspaces from a monorepo, with plans posted as pull request comments by a bot. An engineer removed sensitive = true from a Redis auth token output so the application team could copy it from the apply log.

The token then appeared in three places: the apply log of the CI run, retained for 90 days; the plan comment on the next pull request that touched the module, visible to around 60 people with repository access; and a Slack channel, where someone pasted the log excerpt while debugging an unrelated failure.

Nobody noticed for 11 days. The cleanup took longer than the original change: rotating the token, redeploying four services that cached it, deleting the plan comments, and asking the CI vendor how to purge old logs.

The team skipped the discipline talk. They added a policy check that fails any plan where an output named like *password*, *token* or *secret* lacks the sensitive flag, and they moved the Redis token into a secrets manager so the application read it directly.

The bigger shift is in Terraform itself. Sensitive marking only hides values on screen; ephemeral values and write-only arguments keep them out of plan files and state. A module can now accept an ephemeral password, pass it to a write-only argument, and finish the run with nothing secret persisted.

Provider support for write-only arguments is still uneven, so many teams will run both models side by side for a while: ephemeral where the provider supports it, a secrets manager where it does not, and sensitive outputs only for values that truly must cross a module boundary.

Remote state, OpenTofu and partially sensitive objects

Does terraform_remote_state expose sensitive outputs of another workspace?

Yes. Outputs read through terraform_remote_state keep their values, and anyone with read access to the source state can read them. Treat read access to a state backend as read access to every secret in it.

Does OpenTofu handle sensitive outputs the same way?

The sensitive flag and redaction behave the same way because OpenTofu forked from Terraform 1.5. OpenTofu added client-side state encryption in version 1.7, which protects the stored state file but does not change what the CLI shows on screen.

Can I mark only part of an object as sensitive?

Sensitivity is tracked per value, so a map can contain one sensitive element while the others print normally. Wrapping the whole object in sensitive() marks everything inside it, which is simpler but hides useful non-secret fields from plan output.

Why does my plan show (sensitive value) for a field I never marked?

Providers mark attributes as sensitive in their schemas, and Terraform has propagated those marks since version 0.15. Any expression derived from such an attribute inherits the mark, including string interpolations that mix it with plain text.

The trade-off comes down to convenience against blast radius. Removing the flag saves one command today and puts the secret in every future log. Reading it by name with -raw costs nothing and keeps logs clean, though the value still sits in state. Moving it to a secrets manager or an ephemeral flow costs a refactor and is the only option that takes the secret out of Terraform's hands. For anything that grants access to data, pay for that refactor.