I’m trying to create security group and rules with for_each. Getting the below error while passing the list(string) cidr_blocks value. Any help is much appreciated
╷
│ Error: Unsupported attribute
│
│ on ../../modules/security/main.tf line 28, in resource "aws_security_group" "app_sg":
│ 28: cidr_blocks = egress.value["cidr_blocks"]
│ ├────────────────
│ │ egress.value is object with 3 attributes
│
│ This object does not have an attribute named "cidr_blocks".
╵
Pass the var.egress_rules as is. No need to get the list of values or check if it’s empty. You’re also missing the iterator. By default, it is each, but since you’ve got a dynamic within a higher level for_each I’d recommend creating an interator. The egress dynamic becomes
@dbadrak Thanks for your response. But I got a different error now
│ Error: Invalid index
│
│ on ../../modules/security/main.tf line 30, in resource "aws_security_group" "app_sg":
│ 30: cidr_blocks = egress.value["cidr_blocks"]
│ ├────────────────
│ │ egress.value is object with 3 attributes
│
│ The given key does not identify an element in this collection value.
What I’ve noticed (Terraform 1.1.3) is if the attribute is not defined explicitly in the variable definition, then it gets left out? This makes having psuedo-optional values tough, but there is at least a new experimental feature that allows to configure a variable as optional.