The target group does not have an associated load balancer
Add DependsOn
not just Listener
but also the ListenerRule
like so:
ECSService:
Type: AWS::ECS::Service
DependsOn:
- ListenerHTTPS
- ListenerRule
This case is now covered on the examples section of AWS::ECS::Service CloudFormation page, under "Associate an Application Load Balancer with a service".
The Amazon ECS service requires an explicit dependency on the Application Load Balancer listener rule and the Application Load Balancer listener. This prevents the service from starting before the listener is ready.
The ultimate issue that you have is due to AWS::ECS::Service
trying to attach to the target group before the target group is added to the load balancer. The fix for that is very easy:
Service:
Type: AWS::ECS::Service
DependsOn: Listener # Line Added
Properties:
Cluster: !Ref ECSCluster
Role: 'ecserviceRole'
DesiredCount: '2'
TaskDefinition: !Ref TaskDefinition
LoadBalancers:
- TargetGroupArn: !Ref TargetGroup
ContainerPort: 80
ContainerName: "product-service"
With that being said, you will also have to update your LoadBalancer
definition because it has a lot of errors. It should be:
LoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: 'my-load-balancer'
Subnets:
- !Ref PubSubnetAz1
- !Ref PubSubnetAz2
SecurityGroups:
- !Ref LoadBalancerSecurityGroup