@@ -10,9 +10,11 @@ locals {
1010
1111 assign_public_ip = try (local. task [" assign_public_ip" ], false )
1212
13- container_definition = concat ([
14- for container in module . container_definition :
15- container . json_map_object
13+ container_definition = concat (
14+ [
15+
16+ for container in module . container_definition :
17+ container . json_map_object
1618 ],
1719 [
1820 for container in module . datadog_container_definition :
@@ -129,16 +131,16 @@ locals {
129131 local.container_aliases[item.name] => { container_definition = item }
130132 }
131133
132- containers_priority_terraform = {
134+ containers_priority_terraform = local.enabled ? {
133135 for name , settings in var . containers :
134136 name = > merge (local. container_chamber [name ], lookup (local. container_s3 , name, {}), settings, )
135137 if local . enabled
136- }
137- containers_priority_s3 = {
138+ } : {}
139+ containers_priority_s3 = local.enabled ? {
138140 for name , settings in var . containers :
139141 name = > merge (settings, local. container_chamber [name ], lookup (local. container_s3 , name, {}))
140142 if local . enabled
141- }
143+ } : {}
142144}
143145
144146data "aws_ssm_parameters_by_path" "default" {
@@ -302,27 +304,39 @@ module "ecs_alb_service_task" {
302304
303305 container_definition_json = jsonencode (local. container_definition )
304306
307+ # # When following latest, point service at the latest ACTIVE task definition ARN
308+ # task_definition = local.follow_latest_task_definition ? compact([local.latest_task_definition]) : []
305309 # This is set to true to allow ingress from the ALB sg
306310 use_alb_security_group = local. use_alb_security_group
307311 container_port = local. container_port
308312 alb_security_group = local. lb_sg_id
309- security_group_ids = compact (concat ([local . vpc_sg_id , local . rds_sg_id ], local. external_security_group ))
313+ security_group_ids = compact (concat ([local . vpc_sg_id , local . rds_sg_id ], local. external_security_group , var . additional_security_groups ))
310314 enable_all_egress_rule = var. enable_all_egress_rule
311315
312316 nlb_cidr_blocks = local. is_nlb ? [module . vpc . outputs . vpc_cidr ] : []
313317 nlb_container_port = local. is_nlb ? local. container_port : 80
314318 use_nlb_cidr_blocks = local. is_nlb
315319
316320 # See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service#load_balancer
317- ecs_load_balancers = local. use_lb ? [
318- {
319- container_name = try (local. service_container [" name" ], null ),
320- container_port = local.container_port,
321- target_group_arn = local.is_alb ? module.alb_ingress[0 ].target_group_arn : local.nlb_compat.default_target_group_arn
322- # not required since elb is unused but must be set to null
323- elb_name = null
324- },
325- ] : []
321+ ecs_load_balancers = local. use_lb ? concat (
322+ [
323+ {
324+ container_name = try (local. service_container [" name" ], null ),
325+ container_port = local.container_port,
326+ target_group_arn = local.is_alb ? module.alb_ingress[0 ].target_group_arn : local.nlb_compat.default_target_group_arn
327+ # not required since elb is unused but must be set to null
328+ elb_name = null
329+ },
330+ ],
331+ [
332+ for lb_config in var . additional_lb_target_groups : {
333+ container_name = lb_config . container_name
334+ container_port = lb_config . container_port
335+ target_group_arn = lb_config . target_group_arn
336+ elb_name = null
337+ }
338+ ]
339+ ) : []
326340
327341 assign_public_ip = local. assign_public_ip
328342 ignore_changes_task_definition = try (local. task [" ignore_changes_task_definition" ], false )
@@ -380,7 +394,7 @@ resource "aws_security_group_rule" "custom_sg_rules" {
380394 cidr_blocks = try (each. value . cidr_blocks , null )
381395 source_security_group_id = try (each. value . source_security_group_id , null )
382396 prefix_list_ids = try (each. value . prefix_list_ids , null )
383- security_group_id = one (module. ecs_alb_service_task [* ]. service_security_group_id )
397+ security_group_id = try (each . value . security_group_id , null ) != null ? each . value . security_group_id : one (module. ecs_alb_service_task [* ]. service_security_group_id )
384398}
385399
386400module "alb_ingress" {
@@ -618,8 +632,24 @@ data "aws_ecs_task_definition" "created_task" {
618632
619633locals {
620634 created_task_definition = local. s3_mirroring_enabled ? data. aws_ecs_task_definition . created_task [0 ] : null
635+
636+ # Remove the 'image' field only from the service container to prevent drift when CI/CD updates images
637+ # CI/CD will provide the image for the service container in the complete task definition
638+ # Sidecar containers (datadog, fluent-bit, etc.) retain their images
639+ service_container_name = try (local. service_container [" name" ], null )
640+
641+ # Process each container: strip image from service container, keep it for sidecars
642+ container_definition_without_image = [
643+ for container in local . container_definition : merge (
644+ container ,
645+ container . name == local . service_container_name ? { image = " " } : {}
646+ )
647+ ]
648+
649+
650+ # Build the task template from the Terraform-created task definition
621651 task_template = local. s3_mirroring_enabled ? {
622- containerDefinitions = local.container_definition
652+ containerDefinitions = local.container_definition_without_image
623653 family = lookup (local. created_task_definition , " family" , null ),
624654 taskRoleArn = lookup (local. created_task_definition , " task_role_arn" , null ),
625655 executionRoleArn = lookup (local. created_task_definition , " execution_role_arn" , null ),
@@ -628,11 +658,11 @@ locals {
628658 requiresCompatibilities = [lookup (local. task , " launch_type" , " FARGATE" )]
629659 cpu = tostring (lookup (local. task , " task_cpu" , null ))
630660 memory = tostring (lookup (local. task , " task_memory" , null ))
631-
632661 } : null
662+
633663}
634664
635- resource "aws_s3_bucket_object " "task_definition_template" {
665+ resource "aws_s3_object " "task_definition_template" {
636666 count = local. s3_mirroring_enabled ? 1 : 0
637667 bucket = lookup (module. s3 [0 ]. outputs , " bucket_id" , null )
638668 key = format (" %s/%s/task-template.json" , module. ecs_cluster . outputs . cluster_name , module. this . id )
0 commit comments