0
0
Terraformcloud~10 mins

Optional attributes in objects in Terraform - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an object type with an optional attribute.

Terraform
variable "config" {
  type = object({
    name = string
    age  = number
    city = [1] # optional attribute
  })
}
Drag options to blanks, or click blank then click option'
Astring
Boptional(string)
Coptional(number)
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'string' makes the attribute required.
Using 'optional(number)' for a string attribute causes type mismatch.
2fill in blank
medium

Complete the code to assign a value to the optional attribute in a variable.

Terraform
variable "config" {
  default = {
    name = "Alice"
    age  = 30
    city = [1]
  }
}
Drag options to blanks, or click blank then click option'
A30
Bnull
C"New York"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a number or boolean to a string attribute.
Using null when a string is expected.
3fill in blank
hard

Fix the error in the object type definition by correctly marking the optional attribute.

Terraform
variable "config" {
  type = object({
    name = string
    age  = number
    city = [1]
  })
}
Drag options to blanks, or click blank then click option'
Astring
Bbool
Cnumber
Doptional(string)
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the attribute as just 'string' causes errors if the attribute is missing.
Using wrong types like 'number' or 'bool' for a string attribute.
4fill in blank
hard

Fill both blanks to define an object with one required and one optional attribute.

Terraform
variable "user" {
  type = object({
    username = [1]
    email    = [2]
  })
}
Drag options to blanks, or click blank then click option'
Astring
Boptional(string)
Cnumber
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Marking required attributes as optional.
Using wrong types for attributes.
5fill in blank
hard

Fill all three blanks to create an object with two optional and one required attribute.

Terraform
variable "settings" {
  type = object({
    region   = [1]
    timeout  = [2]
    retries  = [3]
  })
}
Drag options to blanks, or click blank then click option'
Astring
Boptional(number)
Coptional(string)
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up optional and required attributes.
Using wrong types for attributes.