Schema limits and readable alternatives
Read the schema before choosing syntax
The provider defines the shape of the configuration. In an initialized, separately approved and isolated module, terraform providers schema -json describes the schemas of its installed providers. It may load plugin code; do not initialize or inspect an unknown module for this exercise. Instead, open the synthetic provider-schema.json in the lab. Its resource schema contains this abbreviated excerpt.
{ "nesting_mode": "list", "min_items": 1, "max_items": 3, "block": { "attributes": { "name": { "type": "string", "required": true }, "port": { "type": "number", "required": true } } }}This fictional rule accepts one to three children, each requiring a name and a numeric port. The schema version field is not the installed provider version. Check provider version separately. Read attributes for assignable values and block_types for nested blocks; real schemas can also describe nested attributes. Required, optional, computed and sensitive flags have different meanings. The schema alone may not express every provider-specific validation or remote service rule.
Understand the collection shape
A single child represents one object when supplied. A list represents a sequence, so position can matter. A set represents distinct values without a meaningful order; duplicate values may collapse. A map associates keys with child values. These are schema nesting modes, not a choice the caller can arbitrarily change. In particular, changing the order of the input list may change the configuration of a list-nested resource. A map used as dynamic input does not automatically make the output a map-nested block; the schema governs the resulting shape. Do not equate any nested block key with a managed resource address. Inspect actual plan behavior for updates and replacement.
Use an ordinary argument for data values
The teaching schema declares tags as a map-valued attribute. A for expression can build its value directly, and a fixed rule can remain literal. This is a separate illustrative resource; assume var.labels is a map of strings.
resource "training_policy" "office" { name = "office" tags = { for k, v in var.labels : k => v } rule { name = "staff" port = 443 }}Generating dynamic "tags" would not fit this schema because tags is a value argument. Use name = expression syntax for such arguments and literal or generated block syntax for actual nested blocks. Some real providers offer special compatibility syntax; follow their documentation rather than assuming these forms are universally interchangeable.
Handle an optional single child
The teaching schema has a single optional audit block. Assume var.audit is a nullable object with an enabled boolean, not a list. A splat on this nullable single object produces a zero-or-one-element collection.
dynamic "audit" { for_each = var.audit[*] content { enabled = audit.value.enabled }}For null, omit audit. For {enabled = true}, generate one audit block. For {enabled = false}, generate one block with false; it is not the same as omission. This partial snippet does not define the complete variable or resource. In a real security-sensitive interface, confirm the provider default when audit is absent; prefer an explicit setting if omission could weaken the intended policy.
Review the interface, not just the syntax
For two fixed rules, explicit blocks are usually easier to inspect. For a reusable policy with a variable-length list, a dynamic rule may remove duplication. Keep field names meaningful, validate required values, bound the number of entries and explain what omission means. In the worksheet compare a fixed rule with an unnecessarily generated one and identify a rejected count. The offline Python checker tests only the fictional expansion and count rules. It is not HCL parsing, Terraform validation, a provider emulator or evidence of actual access control. Native Terraform test mocking still needs the schema of a real, approved provider; it does not turn a made-up resource type into a supported plugin. A successful mock also does not verify remote enforcement. Continue to the dependency graph chapter to learn how references influence resource ordering.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Schema limits and readable alternatives — knowledge check
Check nested configuration, schema interpretation and readable design.
Where is a map-valued tags argument described in the fixture?
How many rule blocks does the basic schema permit?
Which nesting mode represents an unordered collection of distinct values?
What is generated when the optional audit input is {enabled = false}?
What does the offline schema exercise prove?