CodexBloom - Programming Q&A Platform

AWS CloudFormation Nested Stacks Timeout Error When Creating ECS Service with ALB

👀 Views: 62 💬 Answers: 1 📅 Created: 2025-06-08
aws cloudformation ecs load-balancer YAML

I'm learning this framework and I'm having trouble with a nested CloudFormation stack that creates an ECS service behind an Application Load Balancer (ALB). The main stack creates the ALB and the target group, while a nested stack handles the ECS service creation. The nested stack fails to create the ECS service due to a timeout error, and I see the error message: 'Creating ECS service failed: Target group not ready'. I’ve verified that the target group is created and healthy prior to the ECS service creation, but it seems to be timing out. Here's the relevant part of my CloudFormation template for the main stack: ```yaml Resources: MyALB: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: Name: my-alb Subnets: - !Ref Subnet1 - !Ref Subnet2 SecurityGroups: - !Ref ALBSecurityGroup MyTargetGroup: Type: AWS::ElasticLoadBalancingV2::TargetGroup Properties: Name: my-target-group Port: 80 Protocol: HTTP VpcId: !Ref VPC HealthCheckIntervalSeconds: 30 HealthCheckPath: /health HealthCheckProtocol: HTTP HealthyThresholdCount: 2 UnhealthyThresholdCount: 2 MyEcsService: Type: AWS::CloudFormation::Stack Properties: TemplateURL: <URL-to-nested-stack> Parameters: TargetGroupArn: !Ref MyTargetGroup ``` In the nested stack, I have the ECS service defined like this: ```yaml Resources: MyEcsService: Type: AWS::ECS::Service Properties: Cluster: !Ref MyEcsCluster TaskDefinition: !Ref MyTaskDefinition LoadBalancers: - ContainerName: my-container ContainerPort: 80 TargetGroupArn: !Ref TargetGroupArn DesiredCount: 1 LaunchType: FARGATE NetworkConfiguration: AwsvpcConfiguration: Subnets: - !Ref Subnet1 - !Ref Subnet2 SecurityGroups: - !Ref ECSSecurityGroup AssignPublicIp: ENABLED ``` I’ve tried increasing the `CreationPolicy` to give it more time, but the stack still fails after the default timeout. Is there a way to ensure that the ALB and target group are fully ready before the ECS service attempts to create? Any advice on how to resolve this would be greatly appreciated. I'm using Yaml 3.9 in this project. Has anyone else encountered this?