Type conversion and unknown values
Conversions and unknowns solve different problems
A type constraint is an input contract. Terraform may adapt a supplied value to fit it, if the conversion is supported. An unknown value is different: its eventual contents cannot yet be determined at that point in planning. A value can have a known type and unknown contents. Start with these independent expressions in the console-only fixture. tonumber converts decimal text to a number; tostring requests text.
tonumber("24")tostring(24)tonumber("twenty-four")Expected: number 24, string "24", and a conversion diagnostic, respectively. The third expression is intentionally invalid; run it separately only after the successful examples. Exact diagnostic wording is not verified for the pinned CLI.
What changes at a type boundary
Primitive conversion is context-dependent; it is not permission to mix arbitrary types. A sequence can be accepted by a compatible list constraint, with element conversion as needed. A list cannot fit a fixed tuple of the wrong length. Object conversion may drop surplus attributes, so check that a receiving schema includes information you want to preserve. any asks Terraform to infer a concrete compatible type; it is not a type that permits an arbitrary mixture of values inside one list. Prefer specific contracts when code inspects a value.
The following independent console expressions show why conversion deserves review. jsonencode serializes a value to JSON text and jsondecode reads it back; type is console-only.
tolist(["web", 2])type(jsondecode(jsonencode(toset(["web", "api"]))))Expected: list of strings web and 2; a tuple of two strings, not a set. JSON does not retain Terraform collection distinctions, and external readers may also round large or fractional numbers. Use serialization only when the receiving contract is understood.
Optional attributes and defaults
Read the following declaration from the typed-inventory fixture. optional belongs to the object type constraint, not to an arbitrary expression.
variable "settings" { type = object({ name = string replicas = optional(number, 2) note = optional(string) }) default = { name = "catalog" }}With no overrides, expect name catalog, replicas 2, and note null. If the caller explicitly supplies a null replica attribute, the non-null default still supplies 2. An explicit empty note string remains empty; it is not null. A missing required name is invalid. If the whole variable is null, different variable-level rules apply; do not infer those from attribute defaults. The next chapter explains input assignment and validation.
Follow a value through planning
In a separate disposable fixture, read this complete resource and output body (the inherited version block is in versions.tf).
resource "terraform_data" "label" { input = "catalog"}
output "resolved_label" { value = terraform_data.label.output}Before creation, the computed output can be unknown even though the input is known. A plan may render the result as (known after apply). After a successful local apply, the expected output is catalog. This is a conceptual trace, not captured output. The built-in resource uses local state, not a cloud provider. An unknown is not the string "(known after apply)" and not null. Expressions depending on an unresolved value can remain unresolved, although some results can be determined from other known information. Do not replace unknowns with empty defaults merely to silence a plan error.
Common mistakes and troubleshooting
Separate a conversion error (an incompatible value), null (absence), missing attribute (a structural issue or default opportunity) and unknown (not resolved yet). If a plan cannot determine how many resources to manage, check which inputs are available before apply; resource instance counts must be known during planning. Later chapters cover repetition. Do not use a JSON round trip to assert type identity, or make a guessed placeholder ID a real resource identifier.
Practice and next step
For a new local resource, classify a literal name, an omitted optional note, an unresolved computed output and a failed numeric conversion. Predict what happens to replicas if the optional attribute is explicitly null. Next, declare typed input variables and study their precedence. Related reference: Type conversion and unknown values.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Type conversion and unknown values — knowledge check
Check the concepts and decisions introduced in this lesson.
What is the expected result of tonumber("24")?
What happens to extra attributes when converting a larger object to a narrower compatible object constraint?
For replicas = optional(number, 2), what does an explicit null attribute become?
What does an unknown resource output during planning mean?
Does encoding a set to JSON and decoding it guarantee the original set type?