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
Type constraints in variables
📖 Scenario: You are setting up a Terraform configuration to manage cloud resources. To avoid mistakes, you want to define variables with specific type constraints so that only valid values can be used.
🎯 Goal: Build a Terraform configuration that defines variables with type constraints and uses them in a resource configuration.
📋 What You'll Learn
Define a variable with a string type constraint
Define a variable with a list of strings type constraint
Define a variable with a map of numbers type constraint
Use the variables in a resource block
💡 Why This Matters
🌍 Real World
Defining variables with type constraints helps prevent errors when deploying cloud infrastructure by ensuring only valid data is used.
💼 Career
Cloud engineers and DevOps professionals use Terraform variable type constraints to write reliable and maintainable infrastructure code.
Progress0 / 4 steps
1
Define a string variable
Create a Terraform variable called region with type string and default value "us-west-1".
Terraform
Hint
Use the variable block with type = string and set default to "us-west-1".
2
Define a list of strings variable
Add a Terraform variable called availability_zones with type list(string) and default value ["us-west-1a", "us-west-1b"].
Terraform
Hint
Use list(string) as the type and set the default to the list of zones.
3
Define a map of numbers variable
Add a Terraform variable called instance_counts with type map(number) and default value { web = 2, db = 1 }.
Terraform
Hint
Use map(number) as the type and set the default map with keys web and db.
4
Use variables in a resource
Create a Terraform resource aws_instance named web_server that uses the variable region for availability_zone and the variable instance_counts to set count to the value for key web.
Terraform
Hint
Use count = var.instance_counts["web"] and availability_zone = var.region inside the resource block.
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]