Write dependencies and validate inputs
Read HCL blocks, expressions, lifecycle choices and Terraform 1.12 sensitive-value controls.
Write dependencies and validate inputs
A resource block manages an object while a data source reads information that exists elsewhere. Reference an attribute to express both the value you need and the dependency. Use depends_on for a genuine prerequisite that cannot be inferred. Use create_before_destroy only when the old and new objects can coexist, including any naming or quota constraints.
An input variable describes the caller’s interface. Use types and validation for acceptable values, locals for derived expressions and outputs for values callers should consume. A precondition can block an unsuitable operation. A check block reports a failing assertion as a warning. Sensitive marking hides ordinary display, while ephemeral values and provider-supported write-only arguments address persistence. Verify that every point consuming an ephemeral value supports it. Keep state and saved plans access-controlled.
Use a list when order matters, a set for unique elements without positional meaning, and a map for values indexed by a stable string key. For example, a module can accept a map of service objects and produce normalized names for just the enabled services.
variable "services" {
type = map(object({ name = string, enabled = bool }))
}
locals {
enabled_names = {
for key, service in var.services : key => lower(service.name)
if service.enabled
}
}
With a single enabled entry whose key is api and name is Payments, local.enabled_names["api"] evaluates to "payments". A secret manager such as Vault can issue a short-lived credential. Verify how the value flows through Terraform and the provider as well. Fetching a secret from Vault alone does not prevent an ordinary resource argument from persisting it in state.
Worked case 1
An application receives its database endpoint through a resource reference. Should the configuration also include a blanket depends_on for the whole database module?
Check your reasoning
The endpoint reference already expresses a dependency. A broad explicit dependency can make plans less precise. Add one only for an actual prerequisite that the expression cannot describe.
Worked case 2
The output is marked sensitive, so a teammate proposes sharing state with every developer. Is the value safely absent?
Check your reasoning
Sensitive marking redacts normal display and can still leave the underlying value in state. Control state access and use supported ephemeral or write-only flows for data that must be omitted.
HashiCorp objective references: 4a–4h.
Terraform sensitive and ephemeral data · Official Terraform 004 objectives for this domain
Related lesson in Azure Administrator Associate. Read and preview a Bicep deployment.
Practice with the recording
The team wants an invalid input rejected and a public endpoint checked after deployment. Which failure should stop the run?
Check your reasoning
Reject invalid caller input with variable validation. Use a resource precondition or postcondition for a blocking requirement at the appropriate stage. A check block is suitable when the team wants a warning and continued execution.
sthithapragna · Terraform Associate 004 full course · 2:55:28
Apply the current exam objectives
A sensitive input hides a value from normal display, but that alone does not remove it from persisted state. For a supported provider argument, trace an ephemeral input into a write-only argument. Inspect the resulting plan and state in a disposable local exercise using a synthetic marker. Keep credentials out of the example.
Practice the individual exam skills
- 4a–4d Read an existing network with a data source, create a subnet with a resource and reference an attribute to connect them. Pass a map of named subnets as input and expose only the output the caller needs.
- 4e–4h Use an expression to derive stable instance keys. Prefer references for dependency ordering and add an explicit dependency only when the relationship is otherwise invisible. Test invalid input and trace a short-lived secret from Vault to a supported write-only argument.
Write dependencies and validate inputs quiz
Answer 3 questions. Check each answer for an explanation.