What You'll Learn
You'll learn how to mark Terraform variables as sensitive so their values are hidden from logs and console output, protecting secrets like passwords and API keys from accidental exposure.
Why Sensitive Variables Matter
When Terraform runs, it normally displays variable values in output logs, plan summaries, and error messages. This is helpful for debugging — until you're working with passwords, API tokens, or private keys. Accidentally exposing these secrets in logs or CI/CD pipelines creates serious security risks.
Think of sensitive variables like covering your PIN at an ATM: the system still needs the number to work, but you don't want it displayed on screen where others can see it.
How Sensitive Variables Work
When you mark a variable as sensitive in your variable declaration, Terraform redacts its value wherever it would normally appear. Instead of showing the actual content, you'll see (sensitive value) in plan output, logs, and error messages.
variable "database_password" {
type = string
sensitive = true
}
With this declaration, even when you pass the password via command line or .tfvars files, Terraform will hide it from display. The value is still used internally to provision resources — it's just masked from output.
Important Limitations
Marking a variable as sensitive doesn't encrypt it or prevent it from appearing in your state file. It only controls visibility in Terraform's console output. You still need to:
- Store
.tfvarsfiles securely (never commit them to version control) - Use remote state encryption
- Integrate with proper secrets management tools for production
Key Takeaway: Use sensitive = true in variable declarations to prevent secret values from appearing in Terraform logs and output, but remember this is display masking, not encryption — always combine with proper secrets management practices.