0
0
Terraformcloud~20 mins

Why expressions add logic in Terraform - Challenge Your Understanding

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