0
0
Terraformcloud~10 mins

Type constraints in variables 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 declare a variable that only accepts strings.

Terraform
variable "example_var" {
  type = [1]
}
Drag options to blanks, or click blank then click option'
Abool
Bstring
Cnumber
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' instead of 'string' causes errors when assigning text.
Using 'bool' restricts to true/false only.
2fill in blank
medium

Complete the code to declare a variable that only accepts a list of strings.

Terraform
variable "example_list" {
  type = [1]
}
Drag options to blanks, or click blank then click option'
Aset(number)
Bmap(string)
Clist(string)
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map(string)' expects key-value pairs, not a list.
Using 'string' expects a single string, not a list.
3fill in blank
hard

Fix the error in the variable type declaration to accept a map with string keys and number values.

Terraform
variable "example_map" {
  type = [1]
}
Drag options to blanks, or click blank then click option'
Aobject({key=string})
Bmap(string)
Clist(number)
Dmap(number)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map(string)' expects string values, not numbers.
Using 'object' requires a different syntax and is more complex.
4fill in blank
hard

Fill both blanks to declare a variable that accepts an object with a string 'name' and a number 'age'.

Terraform
variable "person" {
  type = object({
    name = [1],
    age  = [2]
  })
}
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cbool
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the types for 'name' and 'age' causes errors.
Using 'bool' or 'list' types here is incorrect.
5fill in blank
hard

Fill all three blanks to declare a variable that accepts a tuple with a string, a number, and a bool.

Terraform
variable "tuple_var" {
  type = tuple([[1], [2], [3]])
}
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cbool
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' inside tuple is incorrect.
Mixing the order of types causes type errors.