0
0
Terraformcloud~10 mins

Dynamic blocks in security groups 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 security group with a dynamic ingress block.

Terraform
resource "aws_security_group" "example" {
  name        = "example"
  description = "Example security group"

  dynamic "ingress" {
    for_each = var.ingress_rules
    content {
      from_port   = ingress.value.from_port
      to_port     = ingress.value.to_port
      protocol    = ingress.value.protocol
      cidr_blocks = ingress.value.cidr_blocks
    }
  }

  [1] = "vpc-123456"
}
Drag options to blanks, or click blank then click option'
AvpcIdRef
Bvpc
Cvpc_id
DvpcId
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase like vpcId instead of snake_case vpc_id.
Using an undefined attribute like vpc or vpcIdRef.
2fill in blank
medium

Complete the code to loop over ingress rules using a dynamic block.

Terraform
dynamic "ingress" {
  for_each = [1]
  content {
    from_port   = ingress.value.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
Baws_security_group.example.ingress
Clocal.ingress_list
Dmodule.sg.ingress
Attempts:
3 left
💡 Hint
Common Mistakes
Using resource attributes like aws_security_group.example.ingress which is not iterable here.
Using module outputs without proper context.
3fill in blank
hard

Fix the error in the dynamic block to correctly reference the ingress values.

Terraform
dynamic "ingress" {
  for_each = var.ingress_rules
  content {
    from_port   = [1]
    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'
Aingress.value['from_port']
Bingress.from_port
Cvar.ingress_rules.from_port
Dingress.value.from_port
Attempts:
3 left
💡 Hint
Common Mistakes
Using ingress.from_port which is undefined.
Trying to access variable directly instead of current loop item.
4fill in blank
hard

Fill both blanks to define an egress dynamic block similar to ingress.

Terraform
dynamic "egress" {
  for_each = [1]
  content {
    from_port   = egress.value.from_port
    to_port     = egress.value.to_port
    protocol    = egress.value.protocol
    cidr_blocks = [2]
  }
}
Drag options to blanks, or click blank then click option'
Avar.egress_rules
Bvar.ingress_rules
Cegress.value.cidr_blocks
Dingress.value.cidr_blocks
Attempts:
3 left
💡 Hint
Common Mistakes
Using ingress variables inside egress block.
Mixing ingress and egress references.
5fill in blank
hard

Fill all three blanks to create a dynamic block for tags with key and value.

Terraform
dynamic "tags" {
  for_each = [1]
  content {
    key   = tags.value.key
    value = [2]
  }
}

locals {
  tag_map = { for tag in [3] : tag.key => tag.value }
}
Drag options to blanks, or click blank then click option'
Avar.tags_list
Btags.value.value
Dvar.tag_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Accessing tag value incorrectly.