@@ -124,103 +124,54 @@ resource "aws_cloudwatch_log_group" "this" {
124
124
/* Load Balancer */
125
125
/* -------------------------------------------------------------------------- */
126
126
resource "aws_lb_target_group" "this" {
127
- for_each = var . alb_target_group
127
+ count = local . is_create_target_group ? 1 : 0
128
128
129
- name = format (" %s-tg" , substr (format ( " %s " , replace (each . key , " _ " , " - " )), 0 , min (29 , length (format ( " %s " , replace (each . key , " _ " , " - " )) ))))
129
+ name = format (" %s-tg" , substr (local . container_target_group_object . name , 0 , min (29 , length (local . container_target_group_object . name ))))
130
130
131
- port = lookup (each . value , " port " , null )
132
- protocol = lookup (each . value , " protocol " , null )
131
+ port = lookup (local . container_target_group_object , " port_mappings " , null )[ 0 ] . container_port
132
+ protocol = lookup (local . container_target_group_object , " port_mappings " , null )[ 0 ] . container_port == 443 ? " HTTPS " : " HTTP "
133
133
vpc_id = var. vpc_id
134
- target_type = lookup (each . value , " target_type " , " ip" )
134
+ target_type = " ip"
135
135
deregistration_delay = var. target_group_deregistration_delay
136
136
137
137
health_check {
138
- enabled = lookup (each. value . health_check , " enabled" , null )
139
- interval = lookup (each. value . health_check , " interval" , null )
140
- path = lookup (each. value . health_check , " path" , null )
141
- timeout = lookup (each. value . health_check , " timeout" , null )
142
- healthy_threshold = lookup (each. value . health_check , " healthy_threshold" , null )
143
- unhealthy_threshold = lookup (each. value . health_check , " unhealthy_threshold" , null )
144
- matcher = lookup (each. value . health_check , " matcher" , null )
138
+ interval = lookup (var. health_check , " interval" , null )
139
+ path = lookup (var. health_check , " path" , null )
140
+ timeout = lookup (var. health_check , " timeout" , null )
141
+ healthy_threshold = lookup (var. health_check , " healthy_threshold" , null )
142
+ unhealthy_threshold = lookup (var. health_check , " unhealthy_threshold" , null )
143
+ matcher = lookup (var. health_check , " matcher" , null )
145
144
}
146
145
147
- dynamic "stickiness" {
148
- for_each = lookup (each. value , " stickiness" , null ) == null ? [] : [true ]
149
- content {
150
- enabled = lookup (each. value . stickiness , " enabled" , null )
151
- type = lookup (each. value . stickiness , " type" , null )
152
- cookie_name = lookup (each. value . stickiness , " cookie_name" , null )
153
- cookie_duration = lookup (each. value . stickiness , " cookie_duration" , null )
154
- }
155
- }
156
-
157
- tags = merge (local. tags , { " Name" = format (" %s-tg" , substr (format (" %s" , replace (each. key , " _" , " -" )), 0 , min (29 , length (format (" %s" , replace (each. key , " _" , " -" )))))) })
146
+ tags = merge (local. tags , { " Name" = format (" %s-tg" , substr (local. container_target_group_object . name , 0 , min (29 , length (local. container_target_group_object . name )))) })
158
147
}
159
-
160
148
/* ------------------------------ Listener Rule ----------------------------- */
161
149
resource "aws_lb_listener_rule" "this" {
162
- count = length (var . alb_listener_rules )
150
+ count = local . is_create_target_group ? 1 : 0
163
151
164
152
listener_arn = var. alb_listener_arn
165
- priority = lookup (var. alb_listener_rules [count . index ], " alb_priority" , null )
166
-
167
- dynamic "action" {
168
- for_each = [
169
- # for action_rule in var.additional_alb_rule[count.index].actions :
170
- for action_rule in lookup (var. alb_listener_rules [count . index ], " actions" ) :
171
- action_rule
172
- if action_rule . type == " forward"
173
- ]
174
-
175
- content {
176
- type = action. value [" type" ]
177
- target_group_arn = aws_lb_target_group. this [lookup (var. alb_listener_rules [count . index ], " target_group" )]. arn
178
- }
179
- }
153
+ priority = var. alb_priority
180
154
181
- # redirect actions
182
- dynamic "action" {
183
- for_each = [
184
- # for action_rule in var.additional_alb_rule[count.index].actions :
185
- for action_rule in lookup (var. alb_listener_rules [count . index ], " actions" ) :
186
- action_rule
187
- if action_rule . type == " redirect"
188
- ]
189
-
190
- content {
191
- type = action. value [" type" ]
192
- redirect {
193
- host = lookup (action. value , " host" , null )
194
- path = lookup (action. value , " path" , null )
195
- port = lookup (action. value , " port" , null )
196
- protocol = lookup (action. value , " protocol" , null )
197
- query = lookup (action. value , " query" , null )
198
- status_code = action. value [" status_code" ]
199
- }
200
- }
155
+ action {
156
+ type = " forward"
157
+ target_group_arn = aws_lb_target_group. this [0 ]. arn
201
158
}
202
159
203
- # Path Pattern condition
204
- dynamic "condition" {
205
- for_each = length (lookup (var. alb_listener_rules [count . index ], " alb_paths" , [])) > 0 ? [true ] : []
206
- content {
207
- path_pattern {
208
- values = lookup (var. alb_listener_rules [count . index ], " alb_paths" )
209
- }
160
+ condition {
161
+ path_pattern {
162
+ values = var. alb_paths == [] ? [" *" ] : var. alb_paths
210
163
}
211
164
}
212
165
213
- # Host header condition
214
166
dynamic "condition" {
215
- for_each = lookup ( var. alb_listener_rules [ count . index ], " alb_host_header" , null ) == null ? [] : [true ]
167
+ for_each = var. alb_host_header == null ? [] : [true ]
216
168
content {
217
169
host_header {
218
- values = [lookup ( var. alb_listener_rules [ count . index ], " alb_host_header" ) ]
170
+ values = [var . alb_host_header ]
219
171
}
220
172
}
221
173
}
222
174
223
- # http header condition
224
175
dynamic "condition" {
225
176
for_each = var. custom_header_token == " " ? [] : [true ]
226
177
content {
@@ -231,27 +182,8 @@ resource "aws_lb_listener_rule" "this" {
231
182
}
232
183
}
233
184
234
- # Query string condition
235
- dynamic "condition" {
236
- for_each = length (lookup (var. alb_listener_rules [count . index ], " alb_query_strings" , [])) > 0 ? [true ] : []
237
-
238
- content {
239
- dynamic "query_string" {
240
- for_each = [
241
- for query_string in var . alb_listener_rules [count . index ]. alb_query_strings :
242
- query_string
243
- ]
244
- content {
245
- key = lookup (query_string. value , " key" , null )
246
- value = query_string. value [" value" ]
247
- }
248
- }
249
- }
250
- }
251
-
252
185
tags = local. tags
253
186
}
254
-
255
187
/* -------------------------------------------------------------------------- */
256
188
/* Secret */
257
189
/* -------------------------------------------------------------------------- */
@@ -437,12 +369,12 @@ resource "aws_ecs_service" "this" {
437
369
}
438
370
439
371
dynamic "load_balancer" {
440
- for_each = var . alb_target_group
372
+ for_each = local . is_create_target_group ? [ true ] : []
441
373
442
374
content {
443
- target_group_arn = aws_lb_target_group. this [load_balancer . key ]. arn
375
+ target_group_arn = aws_lb_target_group. this [0 ]. arn
444
376
container_name = local. name
445
- container_port = load_balancer . value . port
377
+ container_port = local . container_target_group_object . port_mappings [ 0 ] . container_port
446
378
}
447
379
}
448
380
0 commit comments