What if your cloud passwords were accidentally shared with the whole team? Sensitive variables stop that from happening.
Why Sensitive variable handling in Terraform? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have to manually write down passwords and API keys on sticky notes or in plain text files to share with your team.
Anyone who finds these notes can see your secrets, risking your cloud resources.
Manually managing secrets is slow and risky.
You might accidentally share sensitive info in emails or code repositories.
It's easy to lose track of who has access, leading to security breaches.
Using sensitive variable handling in Terraform keeps secrets hidden.
It marks variables as sensitive so they don't show up in logs or outputs.
This way, your passwords and keys stay safe while your infrastructure is built automatically.
variable "db_password" { type = string } output "db_password" { value = var.db_password }
variable "db_password" { type = string sensitive = true } output "db_password" { value = var.db_password sensitive = true }
You can safely automate cloud setups without risking exposure of your secret keys or passwords.
A company uses sensitive variables to store database passwords in Terraform.
Developers can deploy infrastructure without ever seeing the actual passwords.
Manual secret sharing risks leaks and slows work.
Sensitive variables hide secrets in Terraform outputs and logs.
This keeps your cloud infrastructure secure and automated.
Practice
sensitive = true on a Terraform variable do?Solution
Step 1: Understand the purpose of sensitive attribute
Thesensitive = trueflag tells Terraform to hide the variable's value in output logs and plans to avoid accidental exposure.Step 2: Clarify what it does not do
It does not encrypt the state file or rotate values; those are separate concerns.Final Answer:
It hides the variable's value in Terraform plan and apply outputs. -> Option AQuick Check:
sensitive = true hides output values [OK]
- Thinking sensitive encrypts the state file
- Assuming sensitive makes variables read-only
- Believing sensitive rotates secrets automatically
Solution
Step 1: Recall Terraform block syntax
Terraform uses HCL syntax where attributes inside blocks are separated by new lines without commas or semicolons.Step 2: Identify correct formatting
variable "db_password" { type = string sensitive = true } correctly placessensitive = trueon a new line without commas or semicolons.Final Answer:
variable "db_password" { type = string sensitive = true } -> Option BQuick Check:
HCL uses new lines, no commas or semicolons [OK]
- Adding commas between attributes
- Using semicolons inside blocks
- Putting attributes on the same line without proper syntax
output "db_password" {
value = var.db_password
sensitive = true
}
What will Terraform display when you run terraform output?Solution
Step 1: Understand sensitive outputs behavior
When an output is marked sensitive, Terraform hides its value in the output command to avoid exposing secrets.Step 2: Confirm expected output
Terraform replaces the actual value with(sensitive)text instead of showing the secret.Final Answer:
It will show(sensitive)instead of the password. -> Option AQuick Check:
sensitive output hides value with (sensitive) [OK]
- Expecting actual secret to print
- Thinking sensitive outputs cause errors
- Assuming output is empty string
sensitive = true but when running terraform plan, the secret value still appears. What is the most likely cause?Solution
Step 1: Understand sensitive variable behavior in plans
Terraform hides sensitive variable values in outputs but if the variable is interpolated directly into resource arguments that display in plan, the value can appear.Step 2: Identify cause of exposure
Using sensitive variables in resource arguments that Terraform shows in plan can reveal the secret despite the sensitive flag.Final Answer:
The variable is used directly in a resource argument that prints its value. -> Option CQuick Check:
Direct use in resource can expose sensitive values [OK]
- Assuming sensitive hides all plan values
- Thinking apply is needed to hide values
- Believing variable type affects sensitivity
Solution
Step 1: Protect variable visibility
Marking the variable as sensitive hides it in outputs and plans, reducing accidental exposure.Step 2: Secure outputs and state file
Using sensitive outputs keeps secrets hidden when showing results, and encrypting the state file protects stored secrets.Final Answer:
Mark the variable assensitive = true, use sensitive outputs, and encrypt the Terraform state file. -> Option DQuick Check:
Combine sensitive flag, outputs, and state encryption [OK]
- Printing sensitive variables in outputs
- Storing secrets in plain text variables
- Relying on defaults without encryption
