Terraform interview question · Most asked

How do sensitive variables and outputs work in Terraform, and what are their limitations?

How it works Asked in DevOps Engineer · Cloud Engineer · Site Reliability Engineer (SRE) interviews

Members can save this page, take notes and track progress. See membership · Sign in

Summary

Marking a variable or output as sensitive = true redacts its value from CLI output and plan diffs, but the value is still stored in plain text in the state file. Sensitive does not encrypt — it only hides from the terminal.

Longer Answer

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

output "db_connection_string" {
  value     = "postgres://app:$${var.db_password}@db.internal:5432/app"
  sensitive = true
}

What sensitive does:

  • Replaces the value with (sensitive value) in plan output, apply output and terraform output on the CLI.
  • Propagates: if a sensitive variable flows into a resource argument, that argument is also redacted in the plan.
  • Prevents accidental exposure in CI logs.

What sensitive does NOT do:

  • Encrypt state: the value is stored in plain text in terraform.tfstate. Anyone with read access to the state file can see it.
  • Prevent logging by providers: some providers log API requests that include sensitive values. Terraform cannot control provider-side logging.
  • Stop terraform output -json: the JSON output includes sensitive values in plain text (by design, so automation can read them).

Best practices:

  • Encrypt the state backend (S3 SSE, GCS encryption, Azure Storage encryption).
  • Restrict state backend access with IAM policies.
  • For secrets like database passwords, prefer manage_master_user_password = true (AWS RDS) or external secret managers so the value never enters Terraform at all.
  • Mark outputs as sensitive when they contain or derive from secrets — Terraform will error if a sensitive value flows into a non-sensitive output.

Key Points

  • sensitive = true redacts from CLI output but does NOT encrypt state
  • State stores sensitive values in plain text — encrypt the backend and restrict access
  • Sensitivity propagates: a resource argument using a sensitive variable is also redacted
  • Best approach: keep secrets out of Terraform entirely (managed passwords, external vaults)

Explanation

What interviewers look for

Look for the critical understanding that sensitive ≠ encrypted. Weak answers assume marking a variable sensitive makes it secure. Strong answers explain the state file exposure and recommend keeping secrets out of Terraform where possible.

More Terraform questions

← All Terraform questions

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake