Complete the code to define a security group with a dynamic ingress block.
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" }
The correct attribute to specify the VPC for the security group is vpc_id.
Complete the code to loop over ingress rules using a dynamic block.
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 } }
The dynamic block loops over var.ingress_rules, which is a variable list of ingress rules.
Fix the error in the dynamic block to correctly reference the ingress values.
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 } }
ingress.from_port which is undefined.Inside the dynamic block, the current item is referenced as ingress.value. So ingress.value.from_port is correct.
Fill both blanks to define an egress dynamic block similar to ingress.
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] } }
The egress block loops over var.egress_rules and uses egress.value.cidr_blocks for the CIDR blocks.
Fill all three blanks to create a dynamic block for tags with key and value.
dynamic "tags" { for_each = [1] content { key = tags.value.key value = [2] } } locals { tag_map = { for tag in [3] : tag.key => tag.value } }
The dynamic block loops over var.tags_list. Use tags.value.key for key and tags.value.value for value. The local map comprehension also uses var.tags_list.