Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
✗ Incorrect
The 'bool' type constraint only accepts boolean values: true or false.
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
✗ Incorrect
The syntax 'map(string)' means a map where each value is a 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
✗ Incorrect
Terraform enforces type constraints strictly and will error if types don't match.
Which type constraint allows defining a fixed structure with named attributes in Terraform?
Astring
Bmap
Clist
Dobject
✗ Incorrect
'object' type lets you define a fixed set of named attributes with their own types.
What is the default type of a Terraform variable if no type constraint is specified?
Aany
Bstring
Cnumber
Dbool
✗ Incorrect
If no type is set, Terraform treats the variable as 'any' type, accepting any value.
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.
Practice
(1/5)
1. What is the main purpose of using type constraints in Terraform variables?
easy
A. To assign default values to variables
B. To make variables optional
C. To encrypt variable values
D. To ensure variables only accept specific kinds of data
Solution
Step 1: Understand variable type constraints
Type constraints limit what kind of data a variable can accept, like strings or lists.
Step 2: Identify the purpose
This helps catch errors early by preventing wrong data types from being used.
Final Answer:
To ensure variables only accept specific kinds of data -> Option D
Quick Check:
Type constraints = restrict data type [OK]
Hint: Type constraints restrict variable data types [OK]
Common Mistakes:
Confusing type constraints with default values
Thinking type constraints make variables optional
Assuming type constraints encrypt data
2. Which of the following is the correct syntax to declare a variable with a list of strings type constraint in Terraform?
easy
A. variable "names" { type = list(string) }
B. variable "names" { type = "list of strings" }
C. variable "names" { type = [string] }
D. variable "names" { type = string[] }
Solution
Step 1: Recall Terraform type syntax
Terraform uses list(string) to specify a list of strings as a type.
Step 2: Compare options
variable "names" { type = list(string) } uses the correct syntax. Others use invalid or unsupported formats.
Final Answer:
variable "names" { type = list(string) } -> Option A
Quick Check:
List of strings = list(string) [OK]
Hint: Use list(string) for list of strings type [OK]
Common Mistakes:
Using quotes around type names incorrectly
Using array syntax like string[] which is invalid in Terraform
C. Default value is a list, but type expects a map
D. Variable name cannot be "config"
Solution
Step 1: Analyze the type constraint
The type map(string) expects a map with string values, like { key = "value" }.
Step 2: Check the default value
The default is a list, which does not match the map type.
Final Answer:
Default value is a list, but type expects a map -> Option C
Quick Check:
Type map(string) needs map, not list [OK]
Hint: Match default value type to variable type [OK]
Common Mistakes:
Using list as default for map type
Thinking map(string) is invalid syntax
Believing variable names are restricted
5. You want a variable that accepts either a string or a list of strings. Which type constraint correctly allows this in Terraform?
hard
A. type = string | list(string)
B. type = any
C. type = object({ string_or_list = string })
D. type = string, list(string)
Solution
Step 1: Understand union types in Terraform
Terraform supports union types using the pipe symbol | to allow multiple types.
Step 2: Identify correct syntax
type = string | list(string) uses string | list(string), which means the variable can be either a string or a list of strings.
Step 3: Check other options
type = any allows any type, which is too broad. type = object({ string_or_list = string }) defines an object, not a union. type = string, list(string) is invalid syntax.
Final Answer:
type = string | list(string) -> Option A
Quick Check:
Union type uses | to combine types [OK]
Hint: Use | to combine types for union constraints [OK]