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
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 andterraform outputon 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 = trueredacts 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
- When do you need depends_on in Terraform, and when is it a code smell?
- Someone manually deletes an EC2 instance that Terraform manages. What happens on the next plan and apply?
- How do you configure OIDC authentication between GitHub Actions and AWS for Terraform pipelines?
- How do you use Terraform to deploy the same infrastructure to nonprod and production AWS accounts?
- What are the main terraform state subcommands, and when do you use them?
- Terraform apply fails halfway through — some resources were created, others were not. How do you recover?
- How should nonprod and production Terraform configurations differ, and how do you enforce those differences?
- What is Terraform, and how does it work at a high level?