-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
@antonbabenko @bryantbiggs Thank you |
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 |
There was a problem hiding this 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([]) |
There was a problem hiding this comment.
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" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be
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" { |
There was a problem hiding this comment.
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" { |
There was a problem hiding this comment.
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" { |
There was a problem hiding this comment.
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" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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([]) |
There was a problem hiding this comment.
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" { |
There was a problem hiding this comment.
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" { |
There was a problem hiding this comment.
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" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop
This PR has been automatically marked as stale because it has been open 30 days |
This PR was automatically closed because of stale in 10 days |
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. |
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?
examples/*
to demonstrate and validate my change(s)examples/*
projectspre-commit run -a
on my pull request