0
0
Terraformcloud~5 mins

Type constraints in variables in Terraform - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of type constraints in Terraform variables?
Type constraints ensure that variables only accept values of a specific type, like string, number, list, or map. This helps prevent errors by validating input before applying changes.
Click to reveal answer
beginner
How do you declare a variable with a string type constraint in Terraform?
You declare it like this:
variable "example" {
  type = string
}
This means the variable only accepts string values.
Click to reveal answer
beginner
What happens if you assign a wrong type to a Terraform variable with a type constraint?
Terraform will show an error during plan or apply, stopping the process. This protects your infrastructure from invalid configurations.
Click to reveal answer
intermediate
How can you specify a list of numbers as a variable type in Terraform?
Use the syntax:
variable "numbers" {
  type = list(number)
}
This means the variable must be a list where each item is a number.
Click to reveal answer
intermediate
Explain the difference between 'map(string)' and 'object' type constraints in Terraform variables.
'map(string)' means a collection of key-value pairs where each value is a string. 'object' defines a fixed structure with named attributes and their types, like a small form with specific fields.
Click to reveal answer
Which Terraform variable type constraint accepts only true or false values?
Astring
Blist
Cbool
Dnumber
How do you declare a variable that must be a map with string values in Terraform?
Atype = map(string)
Btype = list(string)
Ctype = object
Dtype = string
What will happen if you assign a list of strings to a variable declared as 'list(number)'?
ATerraform will ignore the type and accept the value
BTerraform will throw an error during plan or apply
CTerraform will convert strings to numbers automatically
DTerraform will treat it as a string variable
Which type constraint allows defining a fixed structure with named attributes in Terraform?
Astring
Bmap
Clist
Dobject
What is the default type of a Terraform variable if no type constraint is specified?
Aany
Bstring
Cnumber
Dbool
Describe how type constraints improve Terraform variable usage and give examples of common types.
Think about how specifying types helps avoid mistakes before applying changes.
You got /7 concepts.
    Explain how to declare a variable for a list of objects with specific attributes in Terraform.
    Consider how to enforce structure inside a list.
    You got /4 concepts.