Skip to content

Commit 1520b34

Browse files
authored
Merge pull request #606 from everclearorg/fix/privatelink-provider
fix: use count instead of for_each in privatelink provider to handle unknown IPs at plan time
2 parents 8690f98 + 4c32f58 commit 1520b34

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

ops/modules/privatelink/provider/main.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# NLB + VPC Endpoint Service in the provider VPC.
22
# Exposes a TCP service over PrivateLink so consumers in other VPCs can reach it.
33

4-
# Service endpoints are typically DNS names; resolve to IP for the NLB target group.
4+
# Resolve target_address to IPs for the NLB target group.
55
# NOTE: IPs are resolved at apply-time only. If the underlying service changes IPs
66
# (e.g. ElastiCache failover), the NLB will still point at stale IPs until the next
77
# Terraform apply. Mitigations:
@@ -45,10 +45,15 @@ resource "aws_lb_target_group" "this" {
4545
}
4646
}
4747

48+
# Use count instead of for_each so that the number of instances is known at plan
49+
# time even when the resolved IPs themselves are not (e.g. target_address comes
50+
# from a resource being created in the same apply). The variable
51+
# target_ip_count tells Terraform how many attachments to create; the actual IP
52+
# values are filled in at apply time.
4853
resource "aws_lb_target_group_attachment" "this" {
49-
for_each = toset(data.dns_a_record_set.target.addrs)
54+
count = var.target_ip_count
5055
target_group_arn = aws_lb_target_group.this.arn
51-
target_id = each.value
56+
target_id = data.dns_a_record_set.target.addrs[count.index]
5257
port = var.target_port
5358
}
5459

ops/modules/privatelink/provider/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ variable "target_address" {
2828
type = string
2929
}
3030

31+
variable "target_ip_count" {
32+
description = "Number of IPs the target_address resolves to. Allows Terraform to plan target group attachments even when the IPs are not yet known. Defaults to 1 (single-node ElastiCache, single-AZ RDS, etc.)."
33+
type = number
34+
default = 1
35+
}
36+
3137
variable "target_port" {
3238
description = "Port of the target service"
3339
type = number

0 commit comments

Comments
 (0)