Skip to content

Commit f89a832

Browse files
committed
chore: enhance VPC endpoint consumer module by transitioning to map-based service handling
Signed-off-by: Ghislain Cheng <ghislain.cheng@zama.ai>
1 parent ab92312 commit f89a832

File tree

2 files changed

+61
-53
lines changed

2 files changed

+61
-53
lines changed

modules/vpc-endpoint-consumer/main.tf

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,34 @@ locals {
2727
# Cluster name for tagging (use provided cluster_name or default)
2828
cluster_name_for_tags = var.cluster_name != null ? var.cluster_name : "mpc-cluster"
2929

30-
# Use the VPC endpoint service names provided by partners
31-
# Note: vpc_endpoint_service_name must be provided directly by the partner
32-
# as AWS auto-generates these names when creating VPC endpoint services
33-
vpc_endpoint_service_names = [
34-
for service in var.party_services : service.vpc_endpoint_service_name
35-
]
30+
# Convert party_services list to map for for_each usage
31+
party_services_map = {
32+
for service in var.party_services : service.party_id => service
33+
}
34+
35+
# Create separate map for services that need Kubernetes services
36+
kube_services_map = {
37+
for service in var.party_services : service.party_id => service
38+
if service.create_kube_service
39+
}
3640

3741
}
3842

3943
# ************************************************************
4044
# VPC interface endpoints to connect to partner MPC services
4145
# ************************************************************
4246
resource "aws_vpc_endpoint" "party_interface_endpoints" {
43-
count = length(var.party_services)
47+
for_each = local.party_services_map
4448

4549
vpc_id = local.vpc_id
46-
service_name = local.vpc_endpoint_service_names[count.index]
50+
service_name = each.value.vpc_endpoint_service_name
4751
vpc_endpoint_type = "Interface"
48-
subnet_ids = length(coalesce(var.party_services[count.index].availability_zones, [])) > 0 && var.cluster_name != null ? [
52+
subnet_ids = length(coalesce(each.value.availability_zones, [])) > 0 && var.cluster_name != null ? [
4953
for subnet_id, subnet in data.aws_subnet.cluster_subnets : subnet_id
50-
if subnet.map_public_ip_on_launch == false && contains(var.party_services[count.index].availability_zones, subnet.availability_zone)
54+
if subnet.map_public_ip_on_launch == false && contains(each.value.availability_zones, subnet.availability_zone)
5155
] : local.subnet_ids
5256
security_group_ids = local.security_group_ids
53-
service_region = var.party_services[count.index].region
57+
service_region = each.value.region
5458

5559
# DNS options
5660
private_dns_enabled = var.private_dns_enabled
@@ -61,14 +65,15 @@ resource "aws_vpc_endpoint" "party_interface_endpoints" {
6165
tags = merge(
6266
var.tags,
6367
{
64-
Name = "${var.name_prefix}-${var.party_services[count.index].name}-interface"
65-
"mpc:partner-service" = var.party_services[count.index].name
66-
"mpc:partner-region" = var.party_services[count.index].region
68+
Name = "${var.name_prefix}-${each.value.name}-interface"
69+
"mpc:partner-service" = each.value.name
70+
"mpc:partner-party" = each.key
71+
"mpc:partner-region" = each.value.region
6772
"mpc:component" = "partner-interface"
6873
"mpc:cluster" = local.cluster_name_for_tags
6974
},
70-
var.party_services[count.index].account_id != null ? {
71-
"mpc:partner-account" = var.party_services[count.index].account_id
75+
each.value.account_id != null ? {
76+
"mpc:partner-account" = each.value.account_id
7277
} : {},
7378
)
7479

@@ -92,34 +97,35 @@ resource "kubernetes_namespace" "partner_namespace" {
9297
# Create Kubernetes services that route to the VPC interface endpoints
9398
# *********************************************************************
9499
resource "kubernetes_service" "party_services" {
95-
count = length([for service in var.party_services : service if service.create_kube_service])
100+
for_each = local.kube_services_map
96101

97102
metadata {
98-
name = "mpc-node-${var.party_services[count.index].party_id}"
103+
name = "mpc-node-${each.key}"
99104
namespace = var.create_namespace ? kubernetes_namespace.partner_namespace[0].metadata[0].name : var.namespace
100105

101106
annotations = merge({
102107
"mpc.io/connection-type" = "partner-interface"
103-
"mpc.io/partner-service" = var.party_services[count.index].name
108+
"mpc.io/partner-service" = each.value.name
109+
"mpc.io/partner-party" = each.key
104110
},
105-
var.party_services[count.index].account_id != null ? {
106-
"mpc.io/partner-account" = var.party_services[count.index].account_id
111+
each.value.account_id != null ? {
112+
"mpc.io/partner-account" = each.value.account_id
107113
} : {},
108-
var.party_services[count.index].kube_service_config.additional_annotations)
114+
each.value.kube_service_config.additional_annotations)
109115

110116
labels = merge({
111-
"app.kubernetes.io/name" = "kms-${var.party_services[count.index].party_id}-core"
112-
"app.kubernetes.io/instance" = "kms-${var.party_services[count.index].party_id}-core"
117+
"app.kubernetes.io/name" = "kms-${each.key}-core"
118+
"app.kubernetes.io/instance" = "kms-${each.key}-core"
113119
"app.kubernetes.io/component" = "mpc-partner-interface"
114120
"app.kubernetes.io/part-of" = "mpc-cluster"
115121
"mpc.io/partner-service" = "true"
116-
}, var.party_services[count.index].kube_service_config.labels)
122+
}, each.value.kube_service_config.labels)
117123
}
118124

119125
spec {
120126
type = "ExternalName"
121-
external_name = aws_vpc_endpoint.party_interface_endpoints[count.index].dns_entry[0].dns_name
122-
session_affinity = var.party_services[count.index].kube_service_config.session_affinity
127+
external_name = aws_vpc_endpoint.party_interface_endpoints[each.key].dns_entry[0].dns_name
128+
session_affinity = each.value.kube_service_config.session_affinity
123129

124130
dynamic "port" {
125131
for_each = concat(
@@ -143,15 +149,15 @@ resource "kubernetes_service" "party_services" {
143149
# Create Route53 private hosted zone records for custom DNS names (in progress,optional)
144150
# **************************************************************************************
145151
resource "aws_route53_record" "partner_dns" {
146-
count = var.create_custom_dns_records ? length(var.party_services) : 0
152+
for_each = var.create_custom_dns_records ? local.party_services_map : {}
147153

148154
zone_id = var.private_zone_id
149-
name = "${var.party_services[count.index].name}.${var.dns_domain}"
155+
name = "${each.value.name}.${var.dns_domain}"
150156
type = "A"
151157

152158
alias {
153-
name = aws_vpc_endpoint.party_interface_endpoints[count.index].dns_entry[0].dns_name
154-
zone_id = aws_vpc_endpoint.party_interface_endpoints[count.index].dns_entry[0].hosted_zone_id
159+
name = aws_vpc_endpoint.party_interface_endpoints[each.key].dns_entry[0].dns_name
160+
zone_id = aws_vpc_endpoint.party_interface_endpoints[each.key].dns_entry[0].hosted_zone_id
155161
evaluate_target_health = true
156162
}
157163
}
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,61 @@
11
output "vpc_interface_endpoint_ids" {
22
description = "IDs of the created VPC interface endpoints"
3-
value = [for endpoint in aws_vpc_endpoint.party_interface_endpoints : endpoint.id]
3+
value = [for endpoint in values(aws_vpc_endpoint.party_interface_endpoints) : endpoint.id]
44
}
55

66
output "vpc_interface_endpoint_dns_names" {
77
description = "DNS names of the created VPC interface endpoints"
8-
value = [for endpoint in aws_vpc_endpoint.party_interface_endpoints : endpoint.dns_entry[0].dns_name]
8+
value = [for endpoint in values(aws_vpc_endpoint.party_interface_endpoints) : endpoint.dns_entry[0].dns_name]
99
}
1010

1111
output "vpc_interface_endpoint_hosted_zone_ids" {
1212
description = "Hosted zone IDs of the created VPC interface endpoints"
13-
value = [for endpoint in aws_vpc_endpoint.party_interface_endpoints : endpoint.dns_entry[0].hosted_zone_id]
13+
value = [for endpoint in values(aws_vpc_endpoint.party_interface_endpoints) : endpoint.dns_entry[0].hosted_zone_id]
1414
}
1515

1616
output "vpc_interface_endpoint_service_names" {
1717
description = "Service names of the created VPC interface endpoints"
18-
value = [for endpoint in aws_vpc_endpoint.party_interface_endpoints : endpoint.service_name]
18+
value = [for endpoint in values(aws_vpc_endpoint.party_interface_endpoints) : endpoint.service_name]
1919
}
2020

2121
output "partner_service_details" {
2222
description = "Detailed information about the partner services and their connections"
2323
value = [
24-
for i, endpoint in aws_vpc_endpoint.party_interface_endpoints : {
25-
service_name = var.party_services[i].name
26-
partner_region = var.party_services[i].region
27-
partner_account_id = var.party_services[i].account_id # Can be null
24+
for party_id, endpoint in aws_vpc_endpoint.party_interface_endpoints : {
25+
party_id = party_id
26+
service_name = local.party_services_map[party_id].name
27+
partner_region = local.party_services_map[party_id].region
28+
partner_account_id = local.party_services_map[party_id].account_id # Can be null
2829
vpc_endpoint_service_name = endpoint.service_name
2930
vpc_interface_endpoint_id = endpoint.id
3031
vpc_interface_dns_name = endpoint.dns_entry[0].dns_name
3132
vpc_interface_hosted_zone_id = endpoint.dns_entry[0].hosted_zone_id
3233
network_interface_ids = endpoint.network_interface_ids
3334
state = endpoint.state
34-
created_kube_service = var.party_services[i].create_kube_service
35-
ports = var.party_services[i].ports
35+
created_kube_service = local.party_services_map[party_id].create_kube_service
36+
ports = local.party_services_map[party_id].ports
3637
}
3738
]
3839
}
3940

4041
output "kubernetes_service_names" {
4142
description = "Names of the created Kubernetes services for partner connections"
4243
value = [
43-
for i, service in kubernetes_service.party_services : service.metadata[0].name
44+
for service in values(kubernetes_service.party_services) : service.metadata[0].name
4445
]
4546
}
4647

4748
output "kubernetes_service_namespaces" {
4849
description = "Namespaces of the created Kubernetes services for partner connections"
4950
value = [
50-
for service in kubernetes_service.party_services : service.metadata[0].namespace
51+
for service in values(kubernetes_service.party_services) : service.metadata[0].namespace
5152
]
5253
}
5354

5455
output "kubernetes_service_external_names" {
5556
description = "External names (VPC interface endpoint DNS) used by the Kubernetes services"
5657
value = [
57-
for service in kubernetes_service.party_services : service.spec[0].external_name
58+
for service in values(kubernetes_service.party_services) : service.spec[0].external_name
5859
]
5960
}
6061

@@ -66,7 +67,7 @@ output "namespace_name" {
6667
output "custom_dns_records" {
6768
description = "Custom DNS records created for the VPC interface endpoints (if enabled)"
6869
value = var.create_custom_dns_records ? [
69-
for i, record in aws_route53_record.partner_dns : {
70+
for record in values(aws_route53_record.partner_dns) : {
7071
name = record.name
7172
type = record.type
7273
zone_id = record.zone_id
@@ -81,9 +82,9 @@ output "connection_summary" {
8182
total_partners = length(var.party_services)
8283
partner_regions = distinct([for service in var.party_services : service.region])
8384
partner_accounts = distinct(compact([for service in var.party_services : service.account_id]))
84-
vpc_interface_endpoints = length(aws_vpc_endpoint.party_interface_endpoints)
85-
kubernetes_services = length(kubernetes_service.party_services)
86-
custom_dns_records = var.create_custom_dns_records ? length(aws_route53_record.partner_dns) : 0
85+
vpc_interface_endpoints = length(values(aws_vpc_endpoint.party_interface_endpoints))
86+
kubernetes_services = length(values(kubernetes_service.party_services))
87+
custom_dns_records = var.create_custom_dns_records ? length(values(aws_route53_record.partner_dns)) : 0
8788
namespace = var.create_namespace && anytrue([for service in var.party_services : service.create_kube_service]) ? kubernetes_namespace.partner_namespace[0].metadata[0].name : var.namespace
8889
}
8990
}
@@ -92,21 +93,22 @@ output "connection_summary" {
9293
output "partner_connection_endpoints" {
9394
description = "Connection endpoints for applications to use when connecting to partner services"
9495
value = {
95-
for i, service in var.party_services : service.name => {
96+
for party_id, service in local.party_services_map : party_id => {
9697
# Primary connection methods
97-
vpc_interface_dns = aws_vpc_endpoint.party_interface_endpoints[i].dns_entry[0].dns_name
98-
kubernetes_service_name = service.create_kube_service ? "${service.name}.${var.create_namespace && anytrue([for s in var.party_services : s.create_kube_service]) ? kubernetes_namespace.partner_namespace[0].metadata[0].name : var.namespace}.svc.cluster.local" : null
99-
custom_dns_name = var.create_custom_dns_records ? "${service.name}.${var.dns_domain}" : null
98+
vpc_interface_dns = aws_vpc_endpoint.party_interface_endpoints[party_id].dns_entry[0].dns_name
99+
kubernetes_service_name = service.create_kube_service ? "mpc-node-${party_id}.${var.create_namespace && anytrue([for s in var.party_services : s.create_kube_service]) ? kubernetes_namespace.partner_namespace[0].metadata[0].name : var.namespace}.svc.cluster.local" : null
100+
custom_dns_name = var.create_custom_dns_records ? "party-${party_id}.${var.dns_domain}" : null
100101

101102
# Service details
103+
service_name = service.name
102104
ports = service.ports
103105
partner_region = service.region
104106
partner_account = service.account_id # Can be null if not provided
105107
partner_name = service.partner_name # Can be null if not provided
106108
connection_type = "vpc-interface"
107109

108110
# Advanced connection options
109-
network_interface_ips = aws_vpc_endpoint.party_interface_endpoints[i].network_interface_ids
111+
network_interface_ips = aws_vpc_endpoint.party_interface_endpoints[party_id].network_interface_ids
110112
}
111113
}
112114
}

0 commit comments

Comments
 (0)