0
0
Terraformcloud~10 mins

Optional attributes in objects in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Optional attributes in objects
Define object type with optional attribute
Create object instance
Include optional
Validate object
Use object safely
This flow shows how an object with optional attributes can be defined, instantiated with or without the optional attribute, validated, and then used safely.
Execution Sample
Terraform
variable "config" {
  type = object({
    name = string
    description = optional(string)
  })
}

output "desc" {
  value = var.config.description
}
This Terraform code defines an object variable with an optional 'description' attribute and outputs its value.
Process Table
StepActionInput ObjectValidation ResultOutput Value
1Define variable type{name: string, description: optional string}Valid type definitionN/A
2Assign object with description{name: "app", description: "My app"}Valid object"My app"
3Output descriptionvar.config.descriptionExists"My app"
4Assign object without description{name: "app"}Valid object (description omitted)null
5Output descriptionvar.config.descriptionMissing optional attributenull
6EndN/AN/AExecution stops
💡 Execution stops after outputting description with or without optional attribute.
Status Tracker
VariableStartAfter Step 2After Step 4Final
var.configundefined{"name": "app", "description": "My app"}{"name": "app"}var.config depends on input
var.config.descriptionundefined"My app"nullnull or string
Key Moments - 2 Insights
Why does the output show null when the optional attribute is omitted?
Because the attribute is optional, Terraform allows it to be missing. When accessed, it returns null as shown in execution_table step 5.
Can the object be invalid if the optional attribute is missing?
No, the object is still valid without the optional attribute, as seen in execution_table step 4 where validation passes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the output value of var.config.description?
Aerror
Bnull
C"My app"
Dundefined
💡 Hint
Refer to execution_table row with Step 3 showing output value.
At which step does the object get assigned without the optional attribute?
AStep 4
BStep 2
CStep 5
DStep 1
💡 Hint
Check execution_table rows for input object assignments.
If the optional attribute is omitted, what value does var.config.description have?
A"My app"
Bnull
Cempty string
Derror
💡 Hint
See variable_tracker for var.config.description after step 4.
Concept Snapshot
Terraform object types can have optional attributes.
Use optional() to mark attributes as optional.
Objects missing optional attributes are still valid.
Accessing missing optional attributes returns null.
This allows flexible object definitions without errors.
Full Transcript
This visual execution shows how Terraform handles optional attributes in object types. First, the object type is defined with an optional attribute. Then, two object instances are created: one with the optional attribute and one without. Both are valid. When the optional attribute is present, its value is output. When omitted, the output is null. This behavior allows safe use of objects with optional fields without causing errors.