@@ -27,30 +27,34 @@ locals {
27
27
# Cluster name for tagging (use provided cluster_name or default)
28
28
cluster_name_for_tags = var. cluster_name != null ? var. cluster_name : " mpc-cluster"
29
29
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
+ }
36
40
37
41
}
38
42
39
43
# ************************************************************
40
44
# VPC interface endpoints to connect to partner MPC services
41
45
# ************************************************************
42
46
resource "aws_vpc_endpoint" "party_interface_endpoints" {
43
- count = length (var . party_services )
47
+ for_each = local . party_services_map
44
48
45
49
vpc_id = local. vpc_id
46
- service_name = local . vpc_endpoint_service_names [ count . index ]
50
+ service_name = each . value . vpc_endpoint_service_name
47
51
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 ? [
49
53
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 )
51
55
] : local. subnet_ids
52
56
security_group_ids = local. security_group_ids
53
- service_region = var . party_services [ count . index ] . region
57
+ service_region = each . value . region
54
58
55
59
# DNS options
56
60
private_dns_enabled = var. private_dns_enabled
@@ -61,14 +65,15 @@ resource "aws_vpc_endpoint" "party_interface_endpoints" {
61
65
tags = merge (
62
66
var. tags ,
63
67
{
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
67
72
" mpc:component" = " partner-interface"
68
73
" mpc:cluster" = local.cluster_name_for_tags
69
74
},
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
72
77
} : {},
73
78
)
74
79
@@ -92,34 +97,35 @@ resource "kubernetes_namespace" "partner_namespace" {
92
97
# Create Kubernetes services that route to the VPC interface endpoints
93
98
# *********************************************************************
94
99
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
96
101
97
102
metadata {
98
- name = " mpc-node-${ var . party_services [ count . index ] . party_id } "
103
+ name = " mpc-node-${ each . key } "
99
104
namespace = var. create_namespace ? kubernetes_namespace. partner_namespace [0 ]. metadata [0 ]. name : var. namespace
100
105
101
106
annotations = merge ({
102
107
" 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
104
110
},
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
107
113
} : {},
108
- var . party_services [ count . index ] . kube_service_config . additional_annotations )
114
+ each . value . kube_service_config . additional_annotations )
109
115
110
116
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"
113
119
" app.kubernetes.io/component" = " mpc-partner-interface"
114
120
" app.kubernetes.io/part-of" = " mpc-cluster"
115
121
" mpc.io/partner-service" = " true"
116
- }, var . party_services [ count . index ] . kube_service_config . labels )
122
+ }, each . value . kube_service_config . labels )
117
123
}
118
124
119
125
spec {
120
126
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
123
129
124
130
dynamic "port" {
125
131
for_each = concat (
@@ -143,15 +149,15 @@ resource "kubernetes_service" "party_services" {
143
149
# Create Route53 private hosted zone records for custom DNS names (in progress,optional)
144
150
# **************************************************************************************
145
151
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 : {}
147
153
148
154
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 } "
150
156
type = " A"
151
157
152
158
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
155
161
evaluate_target_health = true
156
162
}
157
163
}
0 commit comments