@@ -124,54 +124,103 @@ resource "aws_cloudwatch_log_group" "this" {
124
124
/* Load Balancer */
125
125
/* -------------------------------------------------------------------------- */
126
126
resource "aws_lb_target_group" "this" {
127
- count = local . is_create_target_group ? 1 : 0
127
+ for_each = var . alb_target_group
128
128
129
- name = format (" %s-tg" , substr (local . container_target_group_object . name , 0 , min (29 , length (local . container_target_group_object . name ))))
129
+ name = format (" %s-tg" , substr (format ( " %s " , replace (each . key , " _ " , " - " )), 0 , min (29 , length (format ( " %s " , replace (each . key , " _ " , " - " )) ))))
130
130
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 "
131
+ port = lookup (each . value , " port " , null )
132
+ protocol = lookup (each . value , " protocol " , null )
133
133
vpc_id = var. vpc_id
134
- target_type = " ip"
134
+ target_type = lookup (each . value , " target_type " , " ip" )
135
135
deregistration_delay = var. target_group_deregistration_delay
136
136
137
137
health_check {
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 )
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 )
144
145
}
145
146
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 )))) })
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 , " _" , " -" )))))) })
147
158
}
159
+
148
160
/* ------------------------------ Listener Rule ----------------------------- */
149
161
resource "aws_lb_listener_rule" "this" {
150
- count = local . is_create_target_group ? 1 : 0
162
+ count = length (var . alb_listener_rules )
151
163
152
164
listener_arn = var. alb_listener_arn
153
- priority = var. alb_priority
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
+ }
154
180
155
- action {
156
- type = " forward"
157
- target_group_arn = aws_lb_target_group. this [0 ]. arn
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
+ }
158
201
}
159
202
160
- condition {
161
- path_pattern {
162
- values = var. alb_paths == [] ? [" *" ] : var. alb_paths
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
+ }
163
210
}
164
211
}
165
212
213
+ # Host header condition
166
214
dynamic "condition" {
167
- for_each = var. alb_host_header == null ? [] : [true ]
215
+ for_each = lookup ( var. alb_listener_rules [ count . index ], " alb_host_header" , null ) == null ? [] : [true ]
168
216
content {
169
217
host_header {
170
- values = [var . alb_host_header ]
218
+ values = [lookup ( var. alb_listener_rules [ count . index ], " alb_host_header" ) ]
171
219
}
172
220
}
173
221
}
174
222
223
+ # http header condition
175
224
dynamic "condition" {
176
225
for_each = var. custom_header_token == " " ? [] : [true ]
177
226
content {
@@ -182,8 +231,27 @@ resource "aws_lb_listener_rule" "this" {
182
231
}
183
232
}
184
233
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
+
185
252
tags = local. tags
186
253
}
254
+
187
255
/* -------------------------------------------------------------------------- */
188
256
/* Secret */
189
257
/* -------------------------------------------------------------------------- */
@@ -369,12 +437,12 @@ resource "aws_ecs_service" "this" {
369
437
}
370
438
371
439
dynamic "load_balancer" {
372
- for_each = local . is_create_target_group ? [ true ] : []
440
+ for_each = var . alb_target_group
373
441
374
442
content {
375
- target_group_arn = aws_lb_target_group. this [0 ]. arn
443
+ target_group_arn = aws_lb_target_group. this [load_balancer . key ]. arn
376
444
container_name = local. name
377
- container_port = local . container_target_group_object . port_mappings [ 0 ] . container_port
445
+ container_port = load_balancer . value . port
378
446
}
379
447
}
380
448
0 commit comments