Sensitive values and secrets in state

Intermediate · 3 min

Marking a variable or output sensitive hides it from the screen, not from state or plan files. What it protects, what it doesn't, and how to lock down the rest.

Before thisOutputs and localsInside the state file

Infisical · 10 min

Sensitive values and secrets in state

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by InfisicalReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `sensitive = true` on a variable or output makes Terraform print `(sensitive value)` in CLI output and the HCP Terraform UI. It does not stop the value being written to state and plan files in plain text.
  • Anything derived from a sensitive value is treated as sensitive too, so Terraform refuses an output that exposes one unless that output is also marked sensitive.
  • Secrets reach state in two ways: values you pass in, and values a resource generates, such as a `random_password` result or a database's admin password.
  • Protect the file itself: a private, encrypted backend with narrow access, no state or plan files in Git, and read access limited to those who could read the secrets anyway.
  • To keep a secret out of state, not just off the screen, you need ephemeral values and write-only arguments (Terraform 1.10 and 1.11, OpenTofu 1.11), covered in the next lesson.

The password on the screen, and in the file

A team stores a database password in a variable, marks it sensitive, applies, and sees (sensitive value) in the output. The review goes fine and everybody relaxes. Three months later the security team asks where the password is, and the answer is that it is in the state file, in plain text, in a bucket that twelve people can read. The flag did what its documentation says. It was never asked to do the other thing.

What sensitive does

Terraform 0.15 added the sensitive argument to variables and outputs. The docs say it makes Terraform redact those values from CLI output and from the HCP Terraform UI, and that Terraform also treats any expression referencing a sensitive variable or output as sensitive. The effect is mostly helpful: a plan will show (sensitive value) rather than a literal, and Terraform will refuse to print a derived value in an output unless that output is also marked sensitive.

variable "db_password" {
  type      = string
  sensitive = true
}

output "connection_string" {
  value     = "postgres://app:${var.db_password}@${aws_db_instance.main.address}/notes"
  sensitive = true
}

That block is from the docs and I did not run it. It would fail without sensitive = true on the output, because the string includes a sensitive value.

What it doesn’t do

The same docs say the opposite plainly: Terraform state and plan files contain detailed resource attributes and metadata that can include sensitive values such as initial database passwords or API tokens, and local state is a plaintext file that includes any secret values you defined. They tell you to treat the state file as sensitive data.

I checked what that looks like with the state file from the first lesson of this group. I wrote that file by hand in the real format, with a random_password marked sensitive. Then I read it back with jq, the way anyone with access to the bucket could:

$ jq -r '.resources[] | select(.type=="random_password") | .instances[0].attributes.result' terraform.tfstate
Zk3$uPq9!vTn2LwX
$ grep -c 'Zk3' terraform.tfstate
2

Two copies of the value: one in the resource’s attributes and one in the output. The sensitive_attributes list next to the first copy is a note to the CLI. It does not change what is stored. The same holds for plan files saved with -out, which is why those need protecting in CI artifacts. And terraform output -raw db_password prints a sensitive output on purpose, since the whole point of the command is to read it.

How secrets get in

There are two routes, and the second is the one people forget.

  • Values you pass in. A password variable, a token in a provider block, a key in a resource argument. These end up in state if any resource attribute holds them.
  • Values a resource makes. random_password stores its result. A database resource stores the admin password you gave it. Some providers return access keys or connection strings as attributes. You never typed these, and they are in state the moment the resource exists.

The lesson on secrets management in devops covers where secrets should live in general. Here, the point is that Terraform’s state is a copy of whatever it touched.

Protecting the file

If the value must be in state, protect state like the secret it holds.

  • Private, encrypted backend. Block public access, encrypt at rest, version the bucket, and log access. The backends lesson lists the details per cloud.
  • Narrow access. Whoever can read state can read every secret in it. Give read to the CI role and a few people. Do not give it to everyone who can read the code.
  • Nothing in Git. Add *.tfstate* and .terraform/ to .gitignore, and treat saved plans as secrets in CI.
  • Client-side encryption. OpenTofu can encrypt state and plan files itself before they reach the backend, so a bucket reader sees ciphertext. It is an OpenTofu feature, covered in a later lesson.
  • Rotate. A secret that has been in state once, in a bucket others could read, is easier to rotate than to prove unread.

Keeping it out entirely

Redacting and protecting both assume the secret is in state. Newer features avoid that. Per the docs, Terraform 1.10 added the ephemeral argument for variables and child module outputs and the ephemeral block, and 1.11 added write-only arguments on managed resources. OpenTofu 1.11 added ephemeral values, ephemeral resources and write-only attributes. They keep values out of state and plan files. The next lesson covers them.

About the video

The embed is Infisical’s “Terraform Secrets the Right Way: Ephemeral and Write-Only Explained”, published September 2026. I clipped it to the first 3 minutes 25 seconds, from the creator’s own timestamps: the password sitting in the state file, the standard setup with sensitive variables and an encrypted S3 bucket, what each protection actually does, and how Terraform works across config, state and the cloud. The remaining 7 minutes demonstrate ephemeral values and write-only arguments with Infisical, and belong to the next lesson. Infisical sells a secrets manager, so the second half is partly a product demonstration. I judged it from the title, description and chapter list only. Anton Putra’s 2023 video in deeper is a broader tour of secrets in Terraform, from before ephemeral values. English was judged from the title, description and the English auto-caption track.

Where this fits

Marking, protecting and rotating are the defences when a secret has to land in state. The better answer, where the provider supports it, is for the secret never to land there. That is ephemeral resources and write-only arguments.

Check yourself

  1. 01You mark `db_password` as `sensitive`. Where can someone still read the value?

    In the state file, in saved plan files, and with `terraform output -raw db_password` or `-json`. The flag only redacts normal CLI display and the HCP Terraform UI.

  2. 02A resource attribute depends on a sensitive variable. Is the attribute sensitive too?

    Yes. Terraform propagates the mark to expressions that reference a sensitive value, and refuses to output such an attribute without marking the output sensitive.

  3. 03Your state lives in an encrypted S3 bucket. Is the password in it protected?

    Only at rest and from people without access to the bucket. Anyone or any role that can read the state object reads the password in plain text, so access control on the bucket still matters.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark