CodexBloom - Programming Q&A Platform

Terraform 1.4.3: scenarios 'Invalid type for variable' When Using Dynamic Blocks with AWS Security Groups

๐Ÿ‘€ Views: 59 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-08-22
terraform aws security-group HCL

I'm following best practices but I'm converting an old project and This might be a silly question, but I'm working with an scenario while trying to configure an AWS Security Group with dynamic blocks in Terraform 1.4.3... My goal is to create security group rules based on a list of CIDR blocks that I have defined. However, I'm running into an behavior that states `behavior: Invalid type for variable 'ingress_rules': expected set of object, got string`. Hereโ€™s the relevant part of my configuration: ```hcl variable "ingress_rules" { type = set(object({ from_port = number to_port = number protocol = string cidr_blocks = set(string) })) } resource "aws_security_group" "example" { name = "example-sg" description = "A security group for example purposes" 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 } } } ``` I've defined the variable `ingress_rules` as a set of objects, but when I pass the input to this variable, it seems like I'm providing it as a string instead of the expected structure. Hereโ€™s how Iโ€™m currently passing the variable: ```hcl ingress_rules = set("{ from_port=80, to_port=80, protocol="tcp", cidr_blocks=set(["0.0.0.0/0"]) }") ``` I believe the way I'm formatting the input to `ingress_rules` is incorrect. I've tried removing the set notation around the string but then I get an behavior regarding the input format. I also checked the Terraform documentation for dynamic blocks and variable types, but I'm still exploring. Can someone help clarify the correct way to pass this variable? Any insights would be appreciated! Any advice would be much appreciated. My team is using Hcl for this application. Is this even possible?