0
0
Terraformcloud~10 mins

Dynamic block syntax in Terraform - Interactive Code Practice

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

Complete the code to define a dynamic block for security group rules.

Terraform
dynamic "ingress" {
  for_each = var.rules
  content {
    from_port   = ingress.value.from_port
    to_port     = ingress.value.to_port
    protocol    = ingress.value.protocol
    cidr_blocks = ingress.value.[1]
  }
}
Drag options to blanks, or click blank then click option'
Acidr_blocks
Bports
Caddresses
Dranges
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ports' or 'addresses' instead of 'cidr_blocks' causes errors.
Confusing 'cidr_blocks' with 'protocol'.
2fill in blank
medium

Complete the code to iterate over a list of tags using dynamic blocks.

Terraform
resource "aws_instance" "example" {
  ami           = var.ami
  instance_type = var.instance_type

  dynamic "tag" {
    for_each = [1]
    content {
      key   = tag.key
      value = tag.value
    }
  }
}
Drag options to blanks, or click blank then click option'
Avar.security_groups
Bvar.instances
Cvar.subnets
Dvar.tags
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated variables like 'instances' or 'subnets' causes errors.
Not using a variable at all in the for_each expression.
3fill in blank
hard

Fix the error in the dynamic block to correctly reference the iterator variable.

Terraform
dynamic "egress" {
  for_each = var.egress_rules
  content {
    from_port   = [1].from_port
    to_port     = egress.value.to_port
    protocol    = egress.value.protocol
    cidr_blocks = egress.value.cidr_blocks
  }
}
Drag options to blanks, or click blank then click option'
Aegress.key
Begress.value
Cegress
Degress_rules
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'egress' without '.value' causes attribute errors.
Using 'egress.key' instead of 'egress.value'.
4fill in blank
hard

Fill both blanks to create a dynamic block for multiple ingress rules with correct iteration and attribute access.

Terraform
dynamic "ingress" {
  for_each = [1]
  content {
    from_port   = ingress.[2].from_port
    to_port     = ingress.value.to_port
    protocol    = ingress.value.protocol
    cidr_blocks = ingress.value.cidr_blocks
  }
}
Drag options to blanks, or click blank then click option'
Avar.ingress_rules
Bvalue
Ckey
Dvar.egress_rules
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable for for_each.
Using '.key' instead of '.value' to access attributes.
5fill in blank
hard

Fill all three blanks to define a dynamic block for tags with correct iteration, key, and value references.

Terraform
dynamic "tag" {
  for_each = [1]
  content {
    key   = tag.[2]
    value = tag.[3]
  }
}
Drag options to blanks, or click blank then click option'
Avar.tags
Bkey
Cvalue
Dtags
Attempts:
3 left
💡 Hint
Common Mistakes
Using an incorrect variable for for_each.
Mixing up 'key' and 'value' attribute names.