0
0
Terraformcloud~5 mins

Optional attributes in objects in Terraform - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean when an attribute in a Terraform object type is optional?
An optional attribute means you do not have to provide a value for it when defining the object. Terraform will accept the object without that attribute.
Click to reveal answer
beginner
How do you declare an optional attribute in a Terraform object type?
Use the syntax optional(type) for the attribute's type inside the object definition. For example: optional(string).
Click to reveal answer
intermediate
What happens if you omit an optional attribute in a Terraform object when applying a configuration?
Terraform uses the default value if provided, or treats the attribute as absent without error if no default is set.
Click to reveal answer
intermediate
Can optional attributes in Terraform objects have default values?
Yes, optional attributes can have default values defined in the variable declaration, which Terraform uses if the attribute is not set.
Click to reveal answer
beginner
Give an example of a Terraform object type with one required and one optional attribute.
Example: object({ name = string, age = optional(number) }) Here, name is required and age is optional.
Click to reveal answer
How do you mark an attribute as optional in a Terraform object type?
AUse <code>nullable(type)</code>
BUse <code>optional(type)</code>
CUse <code>required(type)</code>
DUse <code>default(type)</code>
What happens if you omit an optional attribute in a Terraform object without a default value?
ATerraform throws an error
BTerraform requires you to set a value
CTerraform assigns a null value automatically
DTerraform treats the attribute as absent
Which of these is a valid Terraform object type with an optional attribute?
Aobject({ id = string, description = optional(string) })
Bobject({ id = optional(string), description = string })
Cobject({ id = required(string), description = string })
Dobject({ id = nullable(string), description = optional(string) })
Can optional attributes in Terraform objects have default values?
ANo, optional attributes cannot have defaults
BOnly required attributes can have defaults
CYes, they can have default values
DDefaults are not supported in Terraform
If an attribute is not marked optional in a Terraform object type, what does that mean?
AThe attribute is required
BThe attribute is optional
CThe attribute is ignored
DThe attribute has a default value
Explain how to define an optional attribute in a Terraform object type and what happens if it is omitted.
Think about how Terraform handles missing values in objects.
You got /3 concepts.
    Describe a practical example where optional attributes in Terraform objects are useful.
    Consider a resource that sometimes needs extra info but not always.
    You got /3 concepts.