Bird
Raised Fist0
Terraformcloud~20 mins

Conditional expressions (ternary) in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Terraform Conditional Expressions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Terraform Conditional Expression Output
What will be the value of instance_type after applying this Terraform configuration snippet?
Terraform
variable "environment" {
  default = "production"
}

locals {
  instance_type = var.environment == "production" ? "t3.large" : "t3.micro"
}
A"t3.large"
B"t3.micro"
C"production"
Dnull
Attempts:
2 left
💡 Hint
Check the condition comparing the environment variable.
❓ Configuration
intermediate
2:00remaining
Terraform Conditional Expression in Resource Count
Given the following Terraform resource snippet, how many instances of aws_instance will be created if var.deploy is false?
Terraform
variable "deploy" {
  default = false
}

resource "aws_instance" "example" {
  count = var.deploy ? 3 : 0
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
A0
B1
C3
DError: count must be a positive integer
Attempts:
2 left
💡 Hint
Look at the count expression and the value of var.deploy.
❓ Architecture
advanced
2:00remaining
Choosing Subnet Based on Environment Using Conditional Expression
In a Terraform module, you want to select a subnet ID based on the environment variable var.env. Which option correctly assigns subnet_id to "subnet-prod123" if var.env is "prod", and to "subnet-dev123" otherwise?
Asubnet_id = var.env ? "subnet-prod123" : "subnet-dev123"
Bsubnet_id = var.env == "prod" ? "subnet-dev123" : "subnet-prod123"
Csubnet_id = var.env == "prod" ? "subnet-prod123" : "subnet-dev123"
Dsubnet_id = var.env = "prod" ? "subnet-prod123" : "subnet-dev123"
Attempts:
2 left
💡 Hint
Check the equality operator and the order of values in the ternary expression.
❓ security
advanced
2:00remaining
Conditional Expression for Enabling Encryption
You want to enable encryption on an AWS S3 bucket only if the variable var.enable_encryption is true. Which Terraform snippet correctly sets the server_side_encryption_configuration block conditionally?
Aserver_side_encryption_configuration = var.enable_encryption ? [] : [{ rule: { apply_server_side_encryption_by_default: { sse_algorithm: "AES256" } } }]
Bserver_side_encryption_configuration = var.enable_encryption ? [{ rule: { apply_server_side_encryption_by_default: { sse_algorithm: "AES256" } } }] : []
Cserver_side_encryption_configuration = var.enable_encryption == true ? [] : null
Dserver_side_encryption_configuration = var.enable_encryption ? null : [{ rule: { apply_server_side_encryption_by_default: { sse_algorithm: "AES256" } } }]
Attempts:
2 left
💡 Hint
Encryption block should be present only when enabled.
✅ Best Practice
expert
2:00remaining
Complex Conditional Expression for Instance Count
Consider this Terraform variable and resource snippet. What is the value of count for aws_instance.example if var.env is "staging" and var.scale_up is true?
Terraform
variable "env" {
  default = "staging"
}

variable "scale_up" {
  default = true
}

resource "aws_instance" "example" {
  count = var.env == "production" ? 5 : (var.scale_up ? 3 : 1)
  ami           = "ami-abc123"
  instance_type = "t3.medium"
}
A0
B5
C1
D3
Attempts:
2 left
💡 Hint
Evaluate the nested conditional expression step by step.

Practice

(1/5)
1. What does this Terraform conditional expression do?
var.is_production ? "prod" : "dev"
easy
A. Returns true or false
B. Always returns "prod"
C. Returns "prod" if var.is_production is true, otherwise "dev"
D. Causes a syntax error

Solution

  1. Step 1: Understand the condition

    The expression checks if var.is_production is true or false.
  2. Step 2: Evaluate the ternary expression

    If true, it returns "prod"; if false, it returns "dev".
  3. Final Answer:

    Returns "prod" if var.is_production is true, otherwise "dev" -> Option C
  4. Quick Check:

    Condition ? true_value : false_value = C [OK]
Hint: Remember: condition ? value_if_true : value_if_false [OK]
Common Mistakes:
  • Thinking it returns a boolean
  • Confusing true and false branches
  • Assuming it always returns the first value
2. Which of these is the correct syntax for a Terraform conditional expression?
easy
A. condition : true_val ? false_val
B. condition ? true_val : false_val
C. condition ? (true_val, false_val)
D. if (condition) then true_val else false_val

Solution

  1. Step 1: Recall Terraform conditional syntax

    Terraform uses the format: condition ? true_val : false_val.
  2. Step 2: Compare options

    Only condition ? true_val : false_val matches this syntax exactly.
  3. Final Answer:

    condition ? true_val : false_val -> Option B
  4. Quick Check:

    Correct ternary syntax = D [OK]
Hint: Look for question mark and colon in correct order [OK]
Common Mistakes:
  • Using if-then-else syntax from other languages
  • Swapping question mark and colon
  • Using commas inside the expression
3. Given this Terraform code:
variable "env" { default = "test" }
output "message" {
  value = var.env == "prod" ? "Deploy to production" : "Deploy to staging"
}

What will be the output value?
medium
A. Error: unknown variable
B. "Deploy to production"
C. "test"
D. "Deploy to staging"

Solution

  1. Step 1: Check variable value

    var.env is set to "test" by default.
  2. Step 2: Evaluate condition

    var.env == "prod" is false, so the false branch "Deploy to staging" is chosen.
  3. Final Answer:

    "Deploy to staging" -> Option D
  4. Quick Check:

    Condition false -> false branch output = A [OK]
Hint: Check variable value before condition evaluation [OK]
Common Mistakes:
  • Assuming default is "prod"
  • Confusing true and false branches
  • Expecting variable value as output
4. Identify the error in this Terraform conditional expression:
output "result" {
  value = var.enabled ? "yes" "no"
}
medium
A. Missing colon ':' between true and false values
B. Variable var.enabled is undefined
C. Quotes around values are incorrect
D. Ternary operator not supported in outputs

Solution

  1. Step 1: Review ternary syntax

    The correct syntax requires a colon ':' between true and false values.
  2. Step 2: Identify missing colon

    The expression has "? \"yes\" \"no\"" missing the ':' separator.
  3. Final Answer:

    Missing colon ':' between true and false values -> Option A
  4. Quick Check:

    Syntax error due to missing ':' = A [OK]
Hint: Check for '?' and ':' pair in ternary expressions [OK]
Common Mistakes:
  • Omitting the colon ':'
  • Misplacing quotes
  • Assuming ternary not allowed in outputs
5. You want to assign a resource name based on environment, but if the environment variable is empty or null, use "default" instead. Which expression correctly does this in Terraform?
var.env == "" || var.env == null ? "default" : var.env
hard
A. var.env == "" || var.env == null ? "default" : var.env
B. var.env == "" && var.env == null ? "default" : var.env
C. var.env ? "default" : var.env
D. var.env == null ? var.env : "default"

Solution

  1. Step 1: Understand the requirement

    If var.env is empty string or null, use "default"; else use var.env.
  2. Step 2: Analyze each option

    var.env == "" || var.env == null ? "default" : var.env checks if var.env is empty or null, then returns "default", else var.env, matching the requirement exactly.
  3. Final Answer:

    var.env == "" || var.env == null ? "default" : var.env -> Option A
  4. Quick Check:

    Check empty or null -> default else env = B [OK]
Hint: Check both empty string and null explicitly in condition [OK]
Common Mistakes:
  • Using && instead of || causing wrong logic
  • Assuming var.env alone checks for empty or null
  • Swapping true and false branches
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard