Challenge - 5 Problems
Terraform Variable Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate2:00remaining
Identify the correct validation block for a variable
Given the following Terraform variable declaration, which validation block correctly ensures the variable 'environment' only accepts the values 'dev', 'staging', or 'prod'?
Terraform
variable "environment" { type = string description = "Deployment environment" validation { condition = ??? error_message = "Environment must be one of: dev, staging, prod." } }
Attempts:
2 left
💡 Hint
Use a function that checks if a list contains a value.
✗ Incorrect
The 'contains' function checks if the list includes the variable value. Option A uses 'contains' correctly. Option A is valid Terraform syntax but less concise. Option A uses 'in' which is not valid in Terraform expressions. Option A uses 'matches' which is not a Terraform function.
❓ service_behavior
intermediate1:30remaining
What happens when a variable fails validation?
In Terraform, if a variable's validation block condition evaluates to false during plan or apply, what is the expected behavior?
Attempts:
2 left
💡 Hint
Think about how Terraform enforces input correctness.
✗ Incorrect
Terraform halts execution and displays the error_message when validation fails to prevent invalid configurations.
❓ Architecture
advanced2:30remaining
Designing variable validation for CIDR blocks
You want to validate a Terraform variable 'network_cidr' to ensure it is a valid CIDR block (e.g., '10.0.0.0/16'). Which validation condition correctly uses Terraform functions to check this?
Attempts:
2 left
💡 Hint
Use 'can' with a function that parses CIDR blocks.
✗ Incorrect
Option A uses 'can' with 'cidrsubnet' which returns a subnet CIDR if input is valid, so it validates the CIDR format. Option A uses 'cidrhost' which expects a valid CIDR but may fail for some inputs. Option A uses regex which is error-prone and not recommended. Option A only checks non-empty string, not CIDR format.
❓ security
advanced2:00remaining
Validating sensitive variable length
You have a sensitive Terraform variable 'api_key' that must be exactly 32 characters long. Which validation block condition correctly enforces this?
Attempts:
2 left
💡 Hint
Use 'can' to safely check length on sensitive variables.
✗ Incorrect
Option C uses 'can' to avoid errors if the variable is null or invalid, then checks length equals 32. Option C may error if var.api_key is null. Option C allows length greater than 32, not exact. Option C uses 'matches' which is not a Terraform function.
✅ Best Practice
expert3:00remaining
Choosing the best validation for a list variable
You have a Terraform variable 'allowed_ports' of type list(number). You want to validate that all ports are between 1024 and 65535 inclusive. Which validation condition correctly enforces this for every element in the list?
Attempts:
2 left
💡 Hint
Use the correct function that returns true if all elements satisfy a condition.
✗ Incorrect
Option D uses 'alltrue' which returns true if all booleans in the list are true. Option D uses 'all' with only one argument, which is invalid syntax as 'all' requires a collection and a condition expression. Option D compares the length of the boolean list comprehension to the original list length, but this always equals regardless of the values. Option D wraps a valid expression in 'can', which is unnecessary.