Scalable Terraform Configuration Design · Topic 1 of 4
From Requirements to Predictable Configuration
Terraform Professional 002: turn business requirements into typed variables and guardrails, keep plans predictable and keep secrets out of state.
- 5 min
- Choose constructs that enforce the requirement: types and validation for inputs, lifecycle for replacement and deletion rules.
optional(type, default)fills a missing object attribute with the default.- Terraform tracks objects by address, so renames need
movedblocks.
What it is
A requirement such as "production gets three replicas and every resource carries a cost center tag" has to become code that enforces it. The professional skill is picking the construct that makes the rule hard to break, not just possible to follow.
Predictable means the same inputs give the same plan, and a refactor does not destroy anything. Secure means secrets stay out of files, plans and state wherever Terraform allows it.
How it works
Mapping requirements to constructs
| Requirement | Construct |
|---|---|
| Different sizes per environment | map(object(...)) variable, read with for_each or a key lookup |
| Reject bad input early | validation block on the variable |
| Same tags everywhere | merge() in a local, or default_tags in the AWS provider |
| A value used in many places | locals |
| Replace without an outage | create_before_destroy in lifecycle |
| Must never be deleted by a run | prevent_destroy in lifecycle |
| Other teams consume a value | output with a description |
Typed inputs carry the rules
A precise type turns a vague requirement into an error at the right moment. Here each environment needs an instance type, replicas are optional, and a limit is enforced before planning starts.
variable "environments" {
type = map(object({
instance_type = string
replicas = optional(number, 1)
}))
validation {
condition = alltrue([for e in values(var.environments) : e.replicas <= 5])
error_message = "Replicas cannot exceed 5."
}
}
A caller who passes { dev = { instance_type = "t3.micro" } } gets replicas = 1, because optional() supplies the default. A caller who leaves out instance_type gets a type error, and replicas = 9 fails the validation with the message above.
Since Terraform 1.9, a validation can refer to other objects as well as its own variable. That lets one input be checked against another.
Habits that keep plans predictable
| Habit | What it prevents |
|---|---|
Pin Terraform and provider versions, commit .terraform.lock.hcl | A new provider release changing plans without a code change |
for_each with stable keys instead of count | Removing one item shifting every later instance |
moved blocks when refactoring | Destroy and recreate after a rename |
No provider blocks in shared modules | Modules tied to one region or account |
terraform fmt and terraform validate in review | Style noise and internal errors reaching a plan |
Style guide layout: main.tf, variables.tf, outputs.tf, providers.tf | Reviewers hunting for declarations |
A rename is the classic case. Without the block below, the plan destroys the old address and creates the new one, because Terraform tracks objects by address.
moved {
from = aws_instance.web
to = aws_instance.web["primary"]
}
Keeping secrets out of artifacts
The three options are not equal, and questions turn on where the value ends up.
| Option | Stored in plan and state | Use it when |
|---|---|---|
sensitive = true | Yes, only redacted in CLI output | The value must be kept but not displayed |
ephemeral = true (Terraform 1.10) | Never stored | The value is needed only during the run |
| Write-only argument (Terraform 1.11) | Never stored | A resource needs a secret that must not persist |
variable "db_password" {
type = string
ephemeral = true
}
resource "aws_db_instance" "main" {
# other arguments omitted
password_wo = var.db_password
password_wo_version = 1
}
Write-only values are not stored, so Terraform cannot compare them between runs. To rotate the password, change password_wo_version, which tells the provider to send the new value.
Provider credentials belong in environment variables or dynamic credentials, not in .tf files. Never commit state or .tfvars files that hold secrets.
How the exam asks about this
- "Refactor a resource into for_each without replacing it." Add a moved block that maps the old address to the new key.
- "The database password must not appear in state, even encrypted." Use an ephemeral variable with a write-only argument;
sensitiveis not enough. - "Reject an unapproved value before any plan is built." Use a variable validation block, not a check block.
What to remember for the exam
- Choose constructs that enforce the requirement: types and validation for inputs, lifecycle for replacement and deletion rules.
optional(type, default)fills a missing object attribute with the default.- Terraform tracks objects by address, so renames need
movedblocks. sensitiveonly redacts output; the value stays in plan and state.- Ephemeral values and write-only arguments are never stored in plan or state.
- Rotate a write-only value by changing its version argument.
prevent_destroy protects only while the block is in the configuration. If someone deletes the resource block along with its lifecycle setting, the next plan can destroy the object.
Common mistakes
Using sensitive to meet a "not in state" requirement. It hides the value on screen and nothing more. Anyone who can read the state can still read the value.
Choosing count for items that change independently. Instances are tracked by position, so removing an early item changes the index of everything after it. Use for_each with stable keys.
Hard coding environment differences in resources. A conditional per resource spreads the rule across the configuration. Put the differences in one typed variable or local map, and read them from there.
Check yourself
A resource is renamed and the plan shows destroy and create. What keeps the object?
Add a
movedblock with the old and new addresses, so Terraform updates the binding in state instead of replacing the object.What happens when a caller omits an attribute declared as
optional(number, 1)?Terraform fills in
1, and the caller's object is still valid. Without a default, an omitted optional attribute becomesnull.How do you change a write-only password on an existing resource?
Supply the new value and change the matching version argument, such as
password_wo_version. The value is not stored, so the version change is what triggers the update.
Topic 1 of 4 in Scalable Terraform Configuration Design
Test yourself on Scalable Terraform Configuration Design
The full HashiCorp Terraform Infrastructure Engineer Professional question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.