CodexBloom - Programming Q&A Platform

Terraform scenarios to update AWS Security Group rules when using count and dynamic blocks

๐Ÿ‘€ Views: 36 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-07
terraform aws security-group HCL

I'm refactoring my project and I recently switched to I've been working on this all day and I'm working on a personal project and After trying multiple solutions online, I still can't figure this out... I'm working with an scenario when trying to update the security group rules in my Terraform configuration that uses `count` and `dynamic` blocks. My configuration is set up to create a security group with multiple rules based on a variable list, but when I attempt to apply changes, Terraform does not seem to recognize the updates to the rules, resulting in stale configurations. Here is a simplified version of my Terraform code: ```hcl variable "security_rules" { type = list(object({ from_port = number to_port = number protocol = string cidr_blocks = list(string) })) } resource "aws_security_group" "example" { name = "example-security-group" vpc_id = aws_vpc.example.id tags = { Name = "example-sg" } dynamic "ingress" { for_each = var.security_rules content { from_port = ingress.value.from_port to_port = ingress.value.to_port protocol = ingress.value.protocol cidr_blocks = ingress.value.cidr_blocks } } } ``` Initially, I had a different set of rules defined, and after updating the `security_rules` variable with new entries, I ran `terraform apply`. However, Terraform reports that no changes are necessary. Hereโ€™s the output I received: ``` No changes. Your infrastructure matches the configuration. ``` I also tried to manually force the refresh by running `terraform refresh`, but it didn't help. I suspect this has to do with how the `dynamic` block is implemented with `count`, but Iโ€™m not entirely sure. Can anyone guide to understand why Terraform isnโ€™t recognizing the updates to my security group rules? Are there any best practices for managing dynamic blocks with `count` in Terraform that I might be missing? Iโ€™m using Terraform version 1.4.0 and the AWS provider version 4.0.0. Thanks in advance for your help! My development environment is Ubuntu. Any help would be greatly appreciated! Is there a better approach? I recently upgraded to Hcl latest. Any advice would be much appreciated. This is for a web app running on Linux. What am I doing wrong?