References, indexing and evaluation errors
Retrieve a value from a collection
A reference identifies an available value. Indexing selects a member from a collection, while attribute access selects a named property. For a fictional service inventory, use an index to select the first service and a key to select a port. The configuration is complete and local-only.
variable "environment" { type = string default = "dev"}
locals { services = [{ name = "catalog" }, { name = "billing" }] ports = { dev = 8080, test = 8081 }}
output "selection" { value = { first = local.services[0].name port = local.ports[var.environment] names = local.services[*].name }}The predicted result is first catalog, port 8080 and names ["catalog", "billing"]. Changing the environment to test selects 8081. The modern splat [*].name extracts the name of each element; it is shorthand for a collection transformation, not a wildcard search.
Follow the reference and use the right selector
Inputs use var.NAME, local values use local.NAME, and child outputs use module.CALL.OUTPUT. Managed resource attributes use a resource type, local resource name and attribute path, as in terraform_data.ticket.id. These references help Terraform infer dependencies, not an imperative execution order. Lists and tuples use zero-based whole-number indices; maps and objects use string keys. Dot notation is convenient for fixed identifier-like attribute names. Bracket notation accepts a key expression, so local.ports[var.environment] uses the selected environment. Sets do not have a positional index; do not convert a set just to rely on an arbitrary first member. A splat over an ordered sequence preserves the corresponding sequence of values. For maps, use a transformation such as a for expression (covered later), rather than expecting splat to traverse map entries. Splat on a single non-null non-collection value wraps that value into a one-element tuple; on null it yields an empty tuple. Prefer explicit checks when the intent is simply to handle a missing input.
Diagnose a missing key or invalid index
These are intentionally invalid console expressions, to be considered separately, not added to the working fixture:
["catalog", "billing"][2] # no third item{ dev = 8080 }["prod"] # absent key{ name = "catalog" }.port # absent attributeThe first repair depends on whether the third item should exist or whether index 1 was intended. For the map, decide whether prod is an unsupported environment or whether a default is genuinely part of the contract. Do not silently hide a typo in a required property. Read the expression and identify the actual value, its type and the requested index before editing the configuration.
Handle optional input without hiding errors
The following separate configuration deliberately accepts an absent service object and an optional port attribute. Terraform fills an omitted optional attribute without an explicit default with null. A conditional checks the object before reading its port, then checks the port itself.
variable "service" { type = object({ port = optional(number) }) default = null}
locals { port = var.service == null ? 8080 : (var.service.port == null ? 8080 : var.service.port)}
output "port" { value = local.port}A null object, an empty object or an object whose port is null produces 8080. An object with port 9090 produces 9090. Do not rely on a Boolean expression such as var.service != null && var.service.port > 0 as a universal way to suppress access errors: evaluation and diagnostics can vary by version and expression. A conditional guard makes the selection explicit, although both branches must still be structurally valid. Later chapters introduce try and can for particular dynamic errors; they are not a blanket substitute for fixing invalid configuration.
Separate null, unknown values and errors
Null is a known absence, an unknown value is a result Terraform cannot yet determine, and an evaluation error means an expression cannot be evaluated as written. They are not interchangeable. This built-in local-resource fixture illustrates an unknown value without using a cloud service. Review its plan only; do not apply it for this lesson.
resource "terraform_data" "ticket" { input = "catalog"}
output "label" { value = "ticket-${terraform_data.ticket.id}"}In a fresh state, the generated ID and derived label are expected to be unknown during planning. This does not mean they are null or invalid. A known missing index, by contrast, should produce a diagnostic. Terraform can defer some checks involving unknown data until more information is available. The ID is not a deterministic example value. Practice: predict the supplied selection for test and prod, then repair the optional-port scenario and explain the three distinct value states. Next: strings, interpolation and templates.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
References, indexing and evaluation errors — knowledge check
Check expression results and evaluation behavior.
Which index selects the first service?
What happens with environment prod in the supplied selection fixture?
What does local.services[*].name extract?
What is the predicted port when the optional service object is null?
What does an unknown generated ID during a fresh plan indicate?