Sensitive Values and State
Terraform can hide sensitive values in its output, but hiding is not the same as protecting. This lesson explains what sensitive marking does and the wider practices for handling secrets.
Learning outcomes
After completing this lesson, you will be able to:
- Explain what sensitive marking does and does not do.
- Describe practices for handling secrets in Terraform.
Marking values sensitive
Setting sensitive = true on a variable or output tells Terraform to redact its value in plan and apply output and in terraform output, where it appears as (sensitive value) or <sensitive>. Sensitivity propagates: any expression that uses a sensitive value is sensitive too, and Terraform requires an output that exposes a sensitive value to be marked sensitive itself. Provider schemas also mark attributes such as passwords as sensitive automatically.
variable "db_password" {
type = string
sensitive = true
}
output "connection_string" {
value = "postgres://admin:${var.db_password}@${aws_db_instance.main.address}/app"
sensitive = true
}The sensitive() function marks a value as sensitive, and nonsensitive() removes the mark when you are certain a derived value is safe to show, such as a hash or a length.
What sensitive does not do
Sensitive marking is about display. The value is still written in plain text to state and to saved plan files, and terraform output -json, terraform output -raw, terraform show -json and terraform state pull reveal it. Anyone who can read state can read every secret in it.
The following diagram compares the managed and ephemeral random_password resources.

Display, not storage
"Marking a variable sensitive keeps it out of the state file" is false. Only ephemeral values and write-only arguments keep values out of state.
Is the secret shown?
Practices for secrets
Handling secrets safely combines several practices. The following list summarises HashiCorp's recommendations.
- Never write secrets as literals in
.tfor committed.tfvarsfiles; supply them through environment variables, such asTF_VAR_db_password, or a secrets manager. - Store state in a remote backend that encrypts it and restricts access, and never commit state or plan files.
- Give provider credentials through environment variables, workload roles or HCP Terraform dynamic credentials, not provider block arguments.
- Use ephemeral values and write-only arguments so that secrets are never persisted.
- Read secrets from a dedicated system such as HashiCorp Vault, preferably through ephemeral resources.
- In HCP Terraform, mark sensitive workspace variables as sensitive so that they cannot be read back.
Handling secrets
Generated passwords
A configuration creates a database password with resource "random_password" "db" and passes random_password.db.result to the database. The plan shows the password as sensitive. Is the password safe?
The decisive question is where the value is stored.
The result of a random_password resource is stored in state, so anyone with state access can read it, even though it is redacted in output. To avoid storing it, use the ephemeral "random_password" resource, which generates a value during the run only, and pass it to the database's write-only password argument, storing a copy in a secrets manager through another write-only argument if applications need to read it.
Is a variable marked sensitive stored in state?
Show answer
Yes, in plain text. Sensitive marking only redacts the value in CLI output.
Exam Tips
Remember for the exam
- sensitive redacts output; values remain in state and plan files.
- Sensitivity propagates to derived values and requires sensitive outputs.
- output -json and -raw reveal sensitive values.
- Protect state; keep secrets out of code.