0
0
Terraformcloud~10 mins

Variable validation blocks 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 add a validation block that ensures the variable is not empty.

Terraform
variable "region" {
  type = string

  validation {
    condition     = [1]
    error_message = "Region cannot be empty."
  }
}
Drag options to blanks, or click blank then click option'
Avar.region == ""
Bvar.region != ""
Cvar.region == null
Dvar.region == 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' causes the validation to fail incorrectly.
Checking for null instead of empty string when the variable is a string.
2fill in blank
medium

Complete the code to validate that the number variable is greater than zero.

Terraform
variable "instance_count" {
  type = number

  validation {
    condition     = [1]
    error_message = "Instance count must be greater than zero."
  }
}
Drag options to blanks, or click blank then click option'
Avar.instance_count > 0
Bvar.instance_count == 0
Cvar.instance_count <= 0
Dvar.instance_count < 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' which would allow zero or negative numbers.
Checking for equality to zero instead of greater than zero.
3fill in blank
hard

Fix the error in the validation condition to check if the string variable is one of allowed values.

Terraform
variable "environment" {
  type = string

  validation {
    condition     = contains(["dev", "staging", "prod"], [1])
    error_message = "Environment must be dev, staging, or prod."
  }
}
Drag options to blanks, or click blank then click option'
Aenvironment_var
Bvar.env
Cvar.environment
Denvironment
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable name without 'var.' prefix causes an error.
Using a wrong variable name that does not exist.
4fill in blank
hard

Fill both blanks to validate a list variable has at least two items and all are strings.

Terraform
variable "zones" {
  type = list(string)

  validation {
    condition     = length([1]) >= 2 && alltrue([for z in [2] : length(z) > 0])
    error_message = "Zones list must have at least two valid strings."
  }
}
Drag options to blanks, or click blank then click option'
Avar.zones
Bzones
Cvar.zone_list
Dzone_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable name without 'var.' prefix.
Using different variable names in the two blanks.
5fill in blank
hard

Fill all three blanks to validate a map variable keys and values meet conditions.

Terraform
variable "tags" {
  type = map(string)

  validation {
    condition     = alltrue([for k, v in [1] : length(k) > 0 && length(v) [2] 0 && v != [3]])
    error_message = "Tags keys and values must be non-empty strings and values cannot be 'none'."
  }
}
Drag options to blanks, or click blank then click option'
Avar.tags
B>
C"none"
Dtags
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable name without 'var.' prefix.
Using wrong comparison operators.
Not quoting the string 'none'.