0
0
Terraformcloud~20 mins

Arguments and expressions in Terraform - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.