Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Move to using for_each for resource blocks #326

Closed
wants to merge 7 commits into from

Conversation

pincher95
Copy link

@pincher95 pincher95 commented Aug 29, 2024

Description

Move the security groups rules resource blocks to using the for_each meta-argument from the count.

Motivation and Context

It is pretty widely understood that using the count meta-argument can cause churn (ref) in some contexts, such as the context in which this module is used.

Breaking Changes

This change will break all security group rules resources based on count. It will cause churn when switching from an old count-based version of the module to a for_each-based version, because the resources are switching from being an ordered list (sg_rule[0], sg_rule[1], etc.) to a map (sg_rule["443-443-tcp"], sg_rule["22-22-tcp"], etc.).

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
# module.security_group.aws_security_group_rule.ingress_with_cidr_blocks["443-443-tcp"] will be created
  + resource "aws_security_group_rule" "ingress_with_cidr_blocks" {
      + cidr_blocks              = [
          + "10.10.0.0/16",
        ]
      + description              = "HTTPS"
      + from_port                = 443
      + id                       = (known after apply)
      + ipv6_cidr_blocks         = []
      + prefix_list_ids          = []
      + protocol                 = "tcp"
      + security_group_id        = (known after apply)
      + security_group_rule_id   = (known after apply)
      + self                     = false
      + source_security_group_id = (known after apply)
      + to_port                  = 443
      + type                     = "ingress"
    }
  • I have executed pre-commit run -a on my pull request

@pincher95
Copy link
Author

@antonbabenko @bryantbiggs
Can you please check this PR.

Thank you

@bryantbiggs
Copy link
Member

I think if we are going to do a breaking change on this module, we should evaluate all potential changes in order to minimize the amount of disruption. For example, I would hope in the next breaking change we would drop the use of aws_security_group_rule and replace it with aws_vpc_security_group_ingress_rule/ aws_vpc_security_group_egress_rule

Copy link
Member

@bryantbiggs bryantbiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just an initial glance - if we are going to do this, we should re-evaluate it from a first principles approach, not just for the sake of changing from count to for_each

# Computed - Security group rules with "cidr_blocks" and it uses list of rules names
resource "aws_security_group_rule" "computed_ingress_rules" {
count = local.create ? var.number_of_computed_ingress_rules : 0
for_each = local.create ? toset(var.ingress_rules) : toset([])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot use toset() in this manner because it will inevitably cause conflicts down the road with computed values as keys

@@ -64,123 +41,70 @@ resource "aws_security_group" "this_name_prefix" {
###################################
# Security group rules with "cidr_blocks" and it uses list of rules names
resource "aws_security_group_rule" "ingress_rules" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be

Suggested change
resource "aws_security_group_rule" "ingress_rules" {
resource "aws_vpc_security_group_ingress_rule" "this" {

protocol = var.rules[var.computed_ingress_rules[count.index]][2]
from_port = var.rules[each.value][0]
to_port = var.rules[each.value][1]
protocol = var.rules[each.value][2]
}

##########################
# Ingress - Maps of rules
##########################
# Security group rules with "source_security_group_id", but without "cidr_blocks" and "self"
resource "aws_security_group_rule" "ingress_with_source_security_group_id" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these can go away to one generic ingress rule resource and one generic egress rule resource

@@ -189,295 +113,98 @@ resource "aws_security_group_rule" "computed_ingress_with_source_security_group_

# Security group rules with "cidr_blocks", but without "ipv6_cidr_blocks", "source_security_group_id" and "self"
resource "aws_security_group_rule" "ingress_with_cidr_blocks" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop

"rule",
"_",
)][2],
var.rules[lookup(each.value, "rule", "_")][2],
)
}

# Security group rules with "self", but without "cidr_blocks" and "source_security_group_id"
resource "aws_security_group_rule" "ingress_with_self" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop

@@ -525,123 +252,69 @@ resource "aws_security_group_rule" "computed_ingress_with_prefix_list_ids" {
##################################
# Security group rules with "cidr_blocks" and it uses list of rules names
resource "aws_security_group_rule" "egress_rules" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resource "aws_security_group_rule" "egress_rules" {
resource "aws_vpc_security_group_egress_rule" "this" {

@@ -525,123 +252,69 @@ resource "aws_security_group_rule" "computed_ingress_with_prefix_list_ids" {
##################################
# Security group rules with "cidr_blocks" and it uses list of rules names
resource "aws_security_group_rule" "egress_rules" {
count = local.create ? length(var.egress_rules) : 0
for_each = local.create ? toset(var.ingress_rules) : toset([])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no toset()

@@ -650,255 +323,98 @@ resource "aws_security_group_rule" "computed_egress_with_source_security_group_i

# Security group rules with "cidr_blocks", but without "ipv6_cidr_blocks", "source_security_group_id" and "self"
resource "aws_security_group_rule" "egress_with_cidr_blocks" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop

"rule",
"_",
)][2],
var.rules[lookup(each.value, "rule", "_")][2],
)
}

# Security group rules with "self", but without "cidr_blocks" and "source_security_group_id"
resource "aws_security_group_rule" "egress_with_self" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop

"protocol",
var.rules[lookup(var.computed_egress_with_self[count.index], "rule", "_")][2],
var.rules[lookup(each.value, "rule", "_")][2],
)
}

# Security group rules with "egress_prefix_list_ids", but without "cidr_blocks", "self" or "source_security_group_id"
resource "aws_security_group_rule" "egress_with_prefix_list_ids" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop

Copy link

github-actions bot commented Oct 6, 2024

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Oct 6, 2024
Copy link

This PR was automatically closed because of stale in 10 days

@github-actions github-actions bot closed this Oct 17, 2024
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants