Lesson 1 of 3 Sensitive Data and Secrets Management

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.

Where a generated password ends up. The result of a random_password resource is stored in state, so anyone with state access can read it even though plans redact it. The ephemeral random_password resource generates a value for the run only, and passing it to a write-only argument keeps it out of state entirely.
Plans redact sensitive values; state still stores them.

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.

Sort into groups

Is the secret shown?

Exam skills 4.8.1

Decide whether each command reveals the value of a sensitive output or variable.

Log in to try this activity Interactive activities are free with an account.

Practices for secrets

Handling secrets safely combines several practices. The following list summarises HashiCorp's recommendations.

  • Never write secrets as literals in .tf or committed .tfvars files; supply them through environment variables, such as TF_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.
Select all that apply

Handling secrets

Exam skills 4.8.1 · 4.8.4

Choose the practices HashiCorp recommends for secrets.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.