Bird
Raised Fist0
Terraformcloud~20 mins

Arguments and expressions in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Terraform Arguments & Expressions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate
2:00remaining
Terraform Expression Output Result
What is the value of the count local value after this Terraform expression is evaluated?
Terraform
variable "environment" {
  default = "prod"
}

variable "instance_count" {
  default = 3
}

locals {
  count = var.environment == "prod" ? var.instance_count * 2 : var.instance_count
}
A0
B3
C6
Dnull
Attempts:
2 left
💡 Hint
Check the condition in the ternary expression and how it affects the count.
❓ service_behavior
intermediate
2:00remaining
Terraform Conditional Argument Behavior
Given this Terraform resource snippet, what will be the value of the enable_monitoring argument when var.enable is false?
Terraform
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  enable_monitoring = var.enable ? true : false
}
Atrue
Bfalse
Cnull
DSyntax error
Attempts:
2 left
💡 Hint
Look at the ternary expression and the value of var.enable.
❓ Architecture
advanced
2:30remaining
Terraform Expression for Dynamic Subnet Selection
Which option correctly uses a Terraform expression to select all subnet IDs from var.subnets where the availability_zone equals var.az?
Terraform
variable "subnets" {
  type = list(object({
    id                = string
    availability_zone = string
  }))
}

variable "az" {
  type = string
}

locals {
  selected_subnets = <EXPRESSION>
}
A[for s in var.subnets : s.id if s.availability_zone == var.az]
B[for s in var.subnets : if s.availability_zone == var.az s.id]
C[for s in var.subnets : s.id where s.availability_zone == var.az]
D[for s in var.subnets : s.id if s.availability_zone = var.az]
Attempts:
2 left
💡 Hint
Terraform uses 'for' expressions with 'if' filters after the colon.
❓ security
advanced
2:00remaining
Terraform Sensitive Variable Expression
What will happen if you try to output a sensitive variable directly in Terraform like this?
Terraform
variable "db_password" {
  type      = string
  sensitive = true
}

output "password" {
  value = var.db_password
}
ATerraform will fail to plan due to sensitive output.
BTerraform will show the password value in the output.
CTerraform will throw a syntax error.
DTerraform will hide the password value and show <sensitive> in the output.
Attempts:
2 left
💡 Hint
Sensitive variables are handled specially in outputs.
✅ Best Practice
expert
3:00remaining
Terraform Expression for Optional Argument with Default
Which option correctly sets an optional argument tags in a resource to use var.tags if defined, or an empty map if not defined?
Terraform
variable "tags" {
  type    = map(string)
  default = null
}

resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  tags = <EXPRESSION>
}
Avar.tags ?? {}
Bvar.tags ? var.tags : {}
Cvar.tags || {}
Dvar.tags != null ? var.tags : {}
Attempts:
2 left
💡 Hint
Terraform uses the null-coalescing operator for default values.

Practice

(1/5)
1. What is the purpose of arguments inside a Terraform resource block?
easy
A. To set specific details or properties for the resource
B. To run commands on the resource after creation
C. To define the cloud provider used
D. To store output values from the resource

Solution

  1. Step 1: Understand resource blocks

    Resource blocks define cloud resources, and arguments inside them specify details like size or name.
  2. Step 2: Identify argument role

    Arguments set the properties that Terraform uses to create or manage the resource.
  3. Final Answer:

    To set specific details or properties for the resource -> Option A
  4. Quick Check:

    Arguments = resource details [OK]
Hint: Arguments define resource details inside blocks [OK]
Common Mistakes:
  • Confusing arguments with outputs
  • Thinking arguments run commands
  • Mixing provider definition with arguments
2. Which of the following is the correct syntax to assign a variable value to an argument in Terraform?
easy
A. name := var.instance_name
B. name => var.instance_name
C. name == var.instance_name
D. name = var.instance_name

Solution

  1. Step 1: Recall Terraform assignment syntax

    Terraform uses single equals sign (=) to assign values to arguments.
  2. Step 2: Match variable usage

    Variables are referenced with var.<variable_name> and assigned using =.
  3. Final Answer:

    name = var.instance_name -> Option D
  4. Quick Check:

    Assignment uses = and var. prefix [OK]
Hint: Use = to assign variables in Terraform [OK]
Common Mistakes:
  • Using := or == instead of =
  • Confusing arrow => with assignment
  • Omitting var. prefix for variables
3. Given the Terraform expression count = var.enable ? 3 : 1 and var.enable = false, what will be the value of count?
medium
A. true
B. 3
C. 1
D. false

Solution

  1. Step 1: Understand the ternary expression

    The expression uses a condition: if var.enable is true, count is 3; else count is 1.
  2. Step 2: Substitute variable value

    Since var.enable is false, the expression returns the value after the colon, which is 1.
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    False condition returns second value [OK]
Hint: Ternary returns value after colon if condition is false [OK]
Common Mistakes:
  • Choosing the first value regardless of condition
  • Confusing boolean with numeric output
  • Ignoring variable value in expression
4. What is wrong with this Terraform argument expression?
size = var.instance_size ? "large" : "small"
assuming var.instance_size is a string variable.
medium
A. Ternary condition requires a boolean, not a string
B. Strings cannot be used in ternary expressions
C. The quotes around "large" and "small" are invalid
D. Variable names cannot be used in expressions

Solution

  1. Step 1: Check ternary condition type

    The condition before ? must be boolean (true/false), but var.instance_size is a string.
  2. Step 2: Identify type mismatch error

    Using a string as condition causes an error because Terraform expects a boolean expression.
  3. Final Answer:

    Ternary condition requires a boolean, not a string -> Option A
  4. Quick Check:

    Condition must be boolean [OK]
Hint: Ternary condition must be true/false, not string [OK]
Common Mistakes:
  • Using strings as conditions
  • Misplacing quotes around values
  • Assuming variables can't be in expressions
5. You want to create multiple AWS EC2 instances only if var.create_instances is true. Which expression correctly sets the count argument to 5 when true, or 0 when false?
hard
A. count = var.create_instances == 5
B. count = var.create_instances ? 5 : 0
C. count = var.create_instances && 5
D. count = var.create_instances || 0

Solution

  1. Step 1: Understand conditional count setting

    We want count to be 5 if var.create_instances is true, else 0.
  2. Step 2: Evaluate each option

    count = var.create_instances ? 5 : 0 uses ternary: if true, 5; else 0. Options B, C, D misuse operators or return booleans.
  3. Final Answer:

    count = var.create_instances ? 5 : 0 -> Option B
  4. Quick Check:

    Ternary sets count correctly [OK]
Hint: Use ternary to set count based on boolean variable [OK]
Common Mistakes:
  • Using boolean expressions instead of numbers for count
  • Confusing && and || operators for count
  • Not using ternary for conditional count