Bird
Raised Fist0
Terraformcloud~10 mins

Conditional expressions (ternary) in Terraform - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign the value based on the condition.

Terraform
variable "environment" {
  default = "production"
}

output "instance_type" {
  value = var.environment == "production" ? [1] : "t2.micro"
}
Drag options to blanks, or click blank then click option'
A"t3.large"
B"t2.large"
C"t3.nano"
D"t2.micro"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the false value in the true condition position.
Forgetting to quote the instance type strings.
2fill in blank
medium

Complete the code to set the bucket versioning based on the environment.

Terraform
resource "aws_s3_bucket" "example" {
  bucket = "my-app-bucket"
  versioning {
    enabled = var.environment == "production" ? [1] : false
  }
}
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
C"true"
D"false"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings "true" or "false" instead of booleans.
Setting the wrong boolean for the production environment.
3fill in blank
hard

Fix the error in the conditional expression for setting the instance count.

Terraform
resource "aws_instance" "web" {
  count = var.environment == "production" ? [1] : 1
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
Drag options to blanks, or click blank then click option'
A3
B"3"
C"1"
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "3" instead of number 3.
Using the wrong number for production count.
4fill in blank
hard

Fill both blanks to set the environment tag based on the environment variable.

Terraform
resource "aws_instance" "app" {
  ami           = "ami-654321"
  instance_type = "t2.medium"

  tags = {
    Environment = var.environment [1] "production" ? [2] : "dev"
  }
}
Drag options to blanks, or click blank then click option'
A==
B!=
C"production"
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of ==.
Using the wrong string value for the true condition.
5fill in blank
hard

Fill all three blanks to create a conditional expression that sets the instance size and count.

Terraform
resource "aws_instance" "db" {
  instance_type = var.environment [1] "production" ? [2] : "t2.small"
  count         = var.environment [3] "production" ? 2 : 1
  ami           = "ami-789012"
}
Drag options to blanks, or click blank then click option'
A==
B"m5.large"
C!=
D"t2.medium"
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == for conditions.
Mixing instance types or counts incorrectly.

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