Collections and structural types
Choose the shape of an inventory
A collection or structural value groups information so you can pass it around as one unit. For a fictional bookstore service, use an ordered sequence for startup tasks, a set for unique roles, and named fields for a server description. Here are five independent console expressions; each function call converts a literal to the requested collection shape. The final two literals retain their structural types.
tolist(["prepare", "start"])toset(["reader", "writer", "reader"])tomap({ owner = "ops", tier = "dev" })["catalog", 3, true]{ name = "catalog", replicas = 3 }Expected: list of strings; set of two distinct strings (no positional meaning); map of strings; a three-position tuple; object with two named fields. These are models, not deployed resources.
Read the five type constraints
list(T) describes a sequence of values of one element type T, with positions and possible repetitions. set(T) describes distinct elements of one type without a promised positional order. map(T) associates string keys with values of one element type. tuple([T1, T2, ...]) fixes the number and type of positions. object({ field = T, ... }) specifies named attributes and their types. T is a placeholder in this explanation, not a Terraform keyword.
Literal brackets build tuples and literal braces build objects; Terraform often converts them in contexts requesting collections. Use the console to distinguish actual inferred types from the constraint a receiving argument requests. Index sequences starting at zero; access maps by key and objects by attribute. Do not index sets.
Example: inspect an ordered list and a set
Enter the following independently in the approved console-only fixture. length counts elements in this example.
type(["web", "api"])type(tolist(["web", "api"]))length(toset(["web", "api", "web"]))Expected: a tuple with two string positions, a list of strings, and the number 2. A set is suitable for membership, not for deciding which server is first. If you truly need order and duplicates, retain the sequence.
Example: define an inventory contract
The following complete variable declaration is also available in the typed-inventory fixture. A variable is a named input; type specifies its accepted shape and default supplies a value when no override is given. The full rules for variables are covered next chapter.
variable "server" { type = object({ name = string replicas = number tags = map(string) }) default = { name = "catalog" replicas = 2 tags = { owner = "ops" } }}The default describes one server configuration with a name, count and tag mapping. It does not declare a resource. In the isolated fixture, var.server.replicas is expected to evaluate to 2 with no overrides, and var.server.tags["owner"] to the string ops. An object is appropriate because its fields play different roles; a map is appropriate for the flexible tag keys. A list of these objects could represent multiple such descriptions.
Common mistakes
An unconverted tuple can hold mixed types; that does not make list(string) a mixed-type container. A set can remove a repeated value, so converting a sequence to a set may destroy information you need. Do not confuse a key called replicas with a numeric index. A fixed tuple is rarely as self-explanatory as a named object for a server description.
Practice and next step
Choose types for ordered backup stages, distinct feature flags, string-valued labels, a fixed pair of text and number, and one service with named settings. Explain why duplicates matter for the backup stages but not for distinct flags. Next, learn how Terraform fits values to these constraints. Related reference: Collections and structural types.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Collections and structural types — knowledge check
Check the concepts and decisions introduced in this lesson.
Which type preserves order and repeated string values?
What is length(toset(["web", "api", "web"])) expected to return?
What is the literal type family of ["api", 2, true] before conversion?
Which constraint best models arbitrary tag keys with text values?
What is the best model for a server name and numeric replica count as named fields?