Sensitive variables in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to process sensitive variables changes as we add more of them in Terraform.
How does the number of sensitive variables affect the work Terraform does?
Analyze the time complexity of defining and using sensitive variables in Terraform.
variable "db_password" {
type = string
sensitive = true
}
variable "api_key" {
type = string
sensitive = true
}
output "db_password" {
value = var.db_password
sensitive = true
}
This code defines sensitive variables and outputs them without showing their values in logs.
When Terraform runs, it processes each sensitive variable to keep its value hidden.
- Primary operation: Handling each sensitive variable's value securely during plan and apply.
- How many times: Once per sensitive variable defined and used.
As you add more sensitive variables, Terraform does more work to keep each one secure.
| Input Size (n) | Approx. Secure Handling Steps |
|---|---|
| 10 | 10 secure handling steps |
| 100 | 100 secure handling steps |
| 1000 | 1000 secure handling steps |
Pattern observation: The work grows directly with the number of sensitive variables.
Time Complexity: O(n)
This means the time to handle sensitive variables grows in a straight line as you add more of them.
[X] Wrong: "Sensitive variables don't affect execution time because they are just hidden values."
[OK] Correct: Each sensitive variable requires extra steps to keep its value secure, so more variables mean more work.
Understanding how sensitive data handling scales helps you design secure and efficient infrastructure code.
"What if we changed sensitive variables to regular variables? How would the time complexity change?"
Practice
What is the main purpose of marking a variable as sensitive in Terraform?
Solution
Step 1: Understand what sensitive means in Terraform
Marking a variable as sensitive tells Terraform not to show its value in command outputs like plan or apply, protecting secrets from accidental exposure.Step 2: Clarify what sensitive does not do automatically
Sensitive does not make the variable read-only, nor does it encrypt the state file automatically. It only hides the value in outputs.Final Answer:
To hide the variable's value from Terraform plan and apply outputs -> Option AQuick Check:
Sensitive hides values in outputs = B [OK]
- Thinking sensitive encrypts the state file
- Confusing sensitive with read-only variables
- Believing sensitive restricts variable usage
Which of the following is the correct way to declare a sensitive variable in Terraform?
variable "db_password" {type = stringsensitive = true}
Solution
Step 1: Recall Terraform variable block syntax
Terraform uses HCL syntax where attributes are set withkey = valuepairs separated by new lines or spaces.Step 2: Identify correct attribute order and syntax
Attributes order does not matter, but semicolons or colons are invalid in HCL. Sosensitive = trueandtype = stringwith equals signs and no semicolons is correct.Final Answer:
variable "db_password" { type = string sensitive = true } -> Option BQuick Check:
Correct HCL syntax uses equals and no semicolons = A [OK]
- Using semicolons or colons instead of equals
- Putting attributes on the same line without proper syntax
- Incorrect attribute order causing confusion
Given this Terraform code snippet, what will be the output of terraform apply regarding the db_password variable?
variable "db_password" {
type = string
sensitive = true
}
output "password_output" {
value = var.db_password
sensitive = true
}Solution
Step 1: Understand sensitive variable and output behavior
Marking a variable and output as sensitive hides their values from Terraform CLI outputs during plan and apply.Step 2: Check if syntax allows sensitive outputs
Terraform supports marking outputs as sensitive to prevent showing secret values. No syntax error occurs.Final Answer:
The password value will be hidden in the output after apply -> Option AQuick Check:
Sensitive outputs hide values in apply output = A [OK]
- Expecting sensitive values to show in outputs
- Thinking sensitive outputs cause syntax errors
- Confusing plan output with apply output
What is wrong with this Terraform variable declaration if the goal is to keep the value secret?
variable "api_key" {
type = string
default = "mysecret"
}Solution
Step 1: Check if variable is marked sensitive
The variable is not marked withsensitive = true, so its value will be shown in outputs and state.Step 2: Validate other options
Terraform does not have asecrettype, default values are allowed, and variable names have no required prefix.Final Answer:
The variable is missingsensitive = trueto hide the value -> Option CQuick Check:
Missing sensitive attribute means value not hidden = C [OK]
- Assuming type secret exists
- Thinking default values are forbidden for secrets
- Believing variable names must have secret prefix
You want to pass a sensitive database password from a Terraform module to the root module without exposing it in any output or logs. Which approach is best?
Solution
Step 1: Protect sensitive data in modules
Marking variables and outputs as sensitive in both the module and root prevents accidental exposure in CLI outputs and logs.Step 2: Avoid insecure practices
Printing secrets in outputs, storing in plain text files, or relying only on state encryption risks exposure.Final Answer:
Mark the variable as sensitive in the module and mark the output as sensitive in the module and root -> Option DQuick Check:
Mark sensitive in variables and outputs to keep secrets safe = D [OK]
- Printing secrets in outputs for debugging
- Storing secrets in plain text files
- Assuming state encryption alone is enough
