Skip to content

Commit

Permalink
Fix references to aws_security_group.node[0] to instead use splat
Browse files Browse the repository at this point in the history
expression and coalesce() to make module more robust and also fix
another failure mode of terraform-aws-modules#568
  • Loading branch information
Tim Black committed Nov 16, 2022
1 parent 1bc86e1 commit 3d68865
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions node_groups.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ locals {
node_sg_name = coalesce(var.node_security_group_name, "${var.cluster_name}-node")
create_node_sg = var.create && var.create_node_security_group

node_security_group_id = local.create_node_sg ? aws_security_group.node[0].id : var.node_security_group_id
# Terraform will evaluate locals even when state is empty or after destroy, so
# it's critical to NOT refer to list elements that won't existing until apply.
# This can be fixed with "splat" syntax, which is lenient to empty lists:
# https://developer.hashicorp.com/terraform/language/expressions/splat#single-values-as-lists
# node_security_group_id = local.create_node_sg ? aws_security_group.node[0].id : var.node_security_group_id
node_security_group_id = local.create_node_sg ? coalescelist(aws_security_group.node[*].id, [""]) : var.node_security_group_id

node_security_group_rules = {
egress_cluster_443 = {
Expand Down Expand Up @@ -171,7 +176,7 @@ resource "aws_security_group_rule" "node" {
for_each = { for k, v in merge(local.node_security_group_rules, var.node_security_group_additional_rules) : k => v if local.create_node_sg }

# Required
security_group_id = aws_security_group.node[0].id
security_group_id = coalescelist(aws_security_group.node[*].id, [""]) # aws_security_group.node[0].id
protocol = each.value.protocol
from_port = each.value.from_port
to_port = each.value.to_port
Expand Down

0 comments on commit 3d68865

Please sign in to comment.