0
0
Terraformcloud~10 mins

Dynamic blocks in ingress rules 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 ingress rules in Terraform.

Terraform
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.[1]
  }
}
Drag options to blanks, or click blank then click option'
Acidr_blocks
Bports
Cprotocols
Drules
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ports' or 'protocols' instead of 'cidr_blocks' causes errors.
Confusing the attribute names inside the dynamic block.
2fill in blank
medium

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

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

  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'
Aaws_security_group.example.ingress
Bvar.egress_rules
Cvar.ingress_rules
Dlocal.ingress_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using egress rules variable instead of ingress rules.
Referencing resource attributes instead of variables.
3fill in blank
hard

Fix the error in the dynamic block by completing the missing attribute.

Terraform
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.[1]
  }
}
Drag options to blanks, or click blank then click option'
Acidr_block
Bblocks
Ccidr
Dcidr_blocks
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular 'cidr_block' causes Terraform to error.
Using incomplete or incorrect attribute names.
4fill in blank
hard

Fill both blanks to create a dynamic ingress block with a condition to only include rules with protocol 'tcp'.

Terraform
dynamic "ingress" {
  for_each = { for idx, rule in var.ingress_rules : idx => rule if rule.[1] == "[2]" }
  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'
Aprotocol
Btcp
Cfrom_port
Dudp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'from_port' instead of 'protocol' for filtering.
Using 'udp' instead of 'tcp' when the question asks for TCP rules.
5fill in blank
hard

Fill all three blanks to define a dynamic ingress block that filters rules with from_port greater than 1024 and protocol 'udp', and maps the port to 'to_port'.

Terraform
dynamic "ingress" {
  for_each = { for idx, rule in var.ingress_rules : idx => rule if rule.[1] > [2] && rule.[3] == "udp" }
  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'
Afrom_port
B1024
Cprotocol
Dto_port
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to_port' instead of 'from_port' for comparison.
Using string '1024' instead of number 1024.
Mixing up protocol attribute names.