0
0
Terraformcloud~10 mins

Nested dynamic blocks 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 start a dynamic block for 'ingress' inside a security group.

Terraform
resource "aws_security_group" "example" {
  name = "example"

  dynamic "[1]" {
    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
    }
  }
}
Drag options to blanks, or click blank then click option'
Aegress
Bingress
Crule
Ddynamic_block
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'egress' instead of 'ingress' for the dynamic block name.
Using a generic name like 'rule' which is not a valid block name here.
2fill in blank
medium

Complete the code to start a nested dynamic block for 'cidr_blocks' inside the 'ingress' dynamic block.

Terraform
resource "aws_security_group" "example" {
  name = "example"

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

      dynamic "[1]" {
        for_each = ingress.value.cidr_blocks
        content {
          cidr_block = cidr_blocks.value
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Acidr_block
Bcidr_blocks
Cblocks
Dcidrs
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural 'cidr_blocks' instead of singular 'cidr_block'.
Using incorrect block names like 'blocks' or 'cidrs'.
3fill in blank
hard

Fix the error in the nested dynamic block to correctly reference the current item in 'for_each'.

Terraform
resource "aws_security_group" "example" {
  name = "example"

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

      dynamic "cidr_block" {
        for_each = ingress.value.cidr_blocks
        content {
          cidr_block = [1]
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Acidr_block.value
Bcidr_blocks.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cidr_blocks.value' which is plural and incorrect.
Using undefined variable names.
4fill in blank
hard

Fill both blanks to correctly define a nested dynamic block for 'ipv6_cidr_blocks' inside 'ingress'.

Terraform
resource "aws_security_group" "example" {
  name = "example"

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

      dynamic "[1]" {
        for_each = ingress.value.ipv6_cidr_blocks
        content {
          cidr_block = [2].value
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aipv6_cidr_block
Bipv6_cidr_blocks
Dipv6_blocks
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural 'ipv6_cidr_blocks' as block name or variable.
Using incorrect variable names like 'ipv6_blocks'.
5fill in blank
hard

Fill all three blanks to create nested dynamic blocks for 'ingress', 'cidr_block', and 'ipv6_cidr_block' inside a security group.

Terraform
resource "aws_security_group" "example" {
  name = "example"

  dynamic "[1]" {
    for_each = var.ingress_rules
    content {
      from_port = ingress.value.from_port
      to_port   = ingress.value.to_port
      protocol  = ingress.value.protocol

      dynamic "[2]" {
        for_each = ingress.value.cidr_blocks
        content {
          cidr_block = [2].value
        }
      }

      dynamic "[3]" {
        for_each = ingress.value.ipv6_cidr_blocks
        content {
          cidr_block = [3].value
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aingress
Bcidr_block
Cipv6_cidr_block
Degress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'egress' instead of 'ingress' for the outer block.
Mixing up 'cidr_block' and 'ipv6_cidr_block' names.