For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/cloudformation-aws-ecs-no-load-balancer-attached.md.
A documentation index is available at /llms.txt.
Amazon ECS services must be configured with a load balancer so traffic is distributed across tasks and a single task does not become a single point of failure for availability and scaling.
For AWS::ECS::Service resources, the Properties.LoadBalancers property must be defined and contain at least one entry. Resources missing this property or where LoadBalancers is an empty array will be flagged.
Each LoadBalancers entry should reference the service port mapping (for example, ContainerName and ContainerPort) or a TargetGroupArn when using an Application Load Balancer or Network Load Balancer so traffic can be routed to task containers.
#this code is a correct code for which the query should not find any resultResources:ECSService:Type:AWS::ECS::ServiceDependsOn:- ListenerProperties:Role:Ref:ECSServiceRoleTaskDefinition:Ref:ECSTaskDefinitionDesiredCount:1LoadBalancers:- TargetGroupArn:Ref:TargetGroupContainerPort:80ContainerName:sample-appCluster:Ref:ECSCluster
#this is a problematic code where the query should report a result(s)Resources:ECSService:Type:AWS::ECS::ServiceDependsOn:- ListenerProperties:Role:Ref:ECSServiceRoleTaskDefinition:Ref:ECSTaskDefinitionDesiredCount:1Cluster:Ref:ECSClusterECSService2:Type:AWS::ECS::ServiceDependsOn:- ListenerProperties:Role:Ref:ECSServiceRoleTaskDefinition:Ref:ECSTaskDefinitionDesiredCount:1LoadBalancers:[]Cluster:Ref:ECSCluster