Bird
Raised Fist0
Terraformcloud~20 mins

Why expressions add logic in Terraform - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Terraform Logic Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
How does a Terraform 'count' expression affect resource creation?

Consider a Terraform resource with a count expression. What is the effect of setting count = var.create_resource ? 1 : 0?

Terraform
variable "create_resource" {
  type    = bool
  default = true
}

resource "aws_s3_bucket" "example" {
  count = var.create_resource ? 1 : 0
  bucket = "my-example-bucket"
  acl    = "private"
}
AThe resource is always created once, ignoring the <code>count</code> expression.
BThe resource is created only if <code>create_resource</code> is true; otherwise, no resource is created.
CTerraform creates multiple resources equal to the boolean value of <code>create_resource</code>.
DThe resource is created twice if <code>create_resource</code> is false.
Attempts:
2 left
💡 Hint

Think about how count controls the number of resource instances.

❓ Architecture
intermediate
2:00remaining
Using 'for' expressions to create multiple resources dynamically

Given a list of subnet CIDRs, which Terraform expression correctly creates one subnet resource per CIDR?

Terraform
variable "subnet_cidrs" {
  type    = list(string)
  default = ["10.0.1.0/24", "10.0.2.0/24"]
}

resource "aws_subnet" "example" {
  count = length(var.subnet_cidrs)
  vpc_id     = aws_vpc.main.id
  cidr_block = ???
}
Acidr_block = var.subnet_cidrs[-1]
Bcidr_block = var.subnet_cidrs[0]
Ccidr_block = var.subnet_cidrs[count]
Dcidr_block = var.subnet_cidrs[count.index]
Attempts:
2 left
💡 Hint

Remember that count.index gives the current resource index.

❓ security
advanced
2:00remaining
Conditional logic in Terraform to restrict security group rules

Which expression correctly adds an ingress rule only if var.enable_ingress is true?

Terraform
variable "enable_ingress" {
  type    = bool
  default = false
}

resource "aws_security_group" "example" {
  name = "example-sg"

  ingress = ???
}
Aingress = [{ from_port = 80, to_port = 80, protocol = "tcp", cidr_blocks = ["0.0.0.0/0"] }]
Bingress = var.enable_ingress or []
Cingress = var.enable_ingress ? [{ from_port = 80, to_port = 80, protocol = "tcp", cidr_blocks = ["0.0.0.0/0"] }] : []
Dingress = var.enable_ingress && [{ from_port = 80, to_port = 80, protocol = "tcp", cidr_blocks = ["0.0.0.0/0"] }]
Attempts:
2 left
💡 Hint

Think about how to use a conditional expression to choose between a list and an empty list.

❓ Configuration
advanced
2:00remaining
Understanding 'lookup' function with conditional expressions

What is the output of this Terraform expression?

Terraform
variable "env" {
  type    = string
  default = "prod"
}

locals {
  config = {
    prod = "production-config"
    dev  = "development-config"
  }

  selected_config = lookup(local.config, var.env, "default-config")
}
Aselected_config = "production-config"
Bselected_config = "development-config"
Cselected_config = "default-config"
Dselected_config = null
Attempts:
2 left
💡 Hint

Check the value of var.env and the keys in local.config.

🧠 Conceptual
expert
2:00remaining
Why do Terraform expressions add logic to infrastructure as code?

Which statement best explains why Terraform expressions add logic to infrastructure definitions?

AThey allow dynamic decisions and conditional resource creation based on variables and environment, making infrastructure flexible and reusable.
BThey replace the need for any variables or modules in Terraform configurations.
CThey automatically fix syntax errors in Terraform code during deployment.
DThey prevent any changes to infrastructure once deployed, ensuring immutability.
Attempts:
2 left
💡 Hint

Think about how expressions influence resource behavior based on inputs.

Practice

(1/5)
1. What is the main purpose of a conditional expression (also called a "why expression") in Terraform?
easy
A. To define variables with fixed values
B. To create loops that repeat resources
C. To choose between two values based on a condition
D. To import external modules automatically

Solution

  1. Step 1: Understand conditional expressions

    Conditional expressions let Terraform pick one of two values depending on a true or false condition.
  2. Step 2: Compare with other Terraform features

    Loops repeat resources, variables hold values, and modules are imported differently, so they don't match the conditional expression purpose.
  3. Final Answer:

    To choose between two values based on a condition -> Option C
  4. Quick Check:

    Conditional expression = choose value [OK]
Hint: Conditional expressions pick values based on true/false [OK]
Common Mistakes:
  • Confusing conditionals with loops
  • Thinking conditionals define variables
  • Mixing conditionals with module imports
2. Which of the following is the correct syntax for a Terraform conditional expression?
easy
A. condition ? true_value : false_value
B. if (condition) { true_value } else { false_value }
C. condition => true_value, false_value
D. switch(condition) { true_value, false_value }

Solution

  1. Step 1: Recall Terraform conditional syntax

    Terraform uses the syntax: condition ? true_value : false_value for conditionals.
  2. Step 2: Identify incorrect options

    Options A, C, and D use syntax styles from other languages or invalid formats not supported by Terraform.
  3. Final Answer:

    condition ? true_value : false_value -> Option A
  4. Quick Check:

    Terraform conditional = ? : syntax [OK]
Hint: Look for question mark and colon in conditional syntax [OK]
Common Mistakes:
  • Using if-else blocks instead of ? :
  • Confusing with arrow or switch syntax
  • Missing colon or question mark
3. Given the Terraform expression: var.is_production ? "prod-server" : "dev-server", what will be the output if var.is_production is false?
medium
A. "dev-server"
B. "prod-server"
C. null
D. Error: invalid expression

Solution

  1. Step 1: Evaluate the condition

    The condition is var.is_production, which is false in this case.
  2. Step 2: Apply the conditional expression

    Since the condition is false, the expression returns the value after the colon, which is "dev-server".
  3. Final Answer:

    "dev-server" -> Option A
  4. Quick Check:

    False condition returns false_value [OK]
Hint: False condition returns value after colon [OK]
Common Mistakes:
  • Choosing true_value instead of false_value
  • Expecting null instead of false branch
  • Thinking it causes an error
4. Identify the error in this Terraform conditional expression: count = var.enable_feature ? 1, 0
medium
A. Conditional expressions cannot be used for count
B. Missing colon between true and false values
C. Variable name is invalid
D. Using comma instead of question mark

Solution

  1. Step 1: Check conditional expression syntax

    The correct syntax requires a colon ':' between true and false values, not a comma.
  2. Step 2: Confirm other parts are valid

    Variable name and use for count are valid; only the separator is wrong.
  3. Final Answer:

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

    Conditional needs ? and : separators [OK]
Hint: Use colon, not comma, between true and false parts [OK]
Common Mistakes:
  • Using comma instead of colon
  • Thinking count cannot use conditionals
  • Assuming variable name is wrong
5. You want to create a resource with a name that depends on environment type. If var.env is "prod", use "server-prod", otherwise use "server-dev". Which expression correctly implements this logic?
hard
A. name = if var.env == "prod" then "server-prod" else "server-dev"
B. name = var.env ? "server-prod" : "server-dev"
C. name = var.env == "prod" : "server-prod" ? "server-dev"
D. name = var.env == "prod" ? "server-prod" : "server-dev"

Solution

  1. Step 1: Check condition and syntax

    The condition must compare var.env to "prod" using ==, then use ? and : for true/false values.
  2. Step 2: Validate each option

    name = var.env == "prod" ? "server-prod" : "server-dev" uses correct syntax. name = var.env ? "server-prod" : "server-dev" misses comparison. name = var.env == "prod" : "server-prod" ? "server-dev" swaps ? and :. name = if var.env == "prod" then "server-prod" else "server-dev" uses invalid if-then-else syntax in Terraform.
  3. Final Answer:

    name = var.env == "prod" ? "server-prod" : "server-dev" -> Option D
  4. Quick Check:

    Use == with ? : for conditionals [OK]
Hint: Compare with ==, then use ? : for true/false [OK]
Common Mistakes:
  • Omitting comparison operator
  • Swapping ? and : positions
  • Using if-then-else syntax not supported
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