Search code examples
amazon-web-servicesamazon-ecsaws-application-load-balancerecs-taskdefinition

Error while configuring Application loadbalancer in ECS using cloudformation


I am creating ECS cluster, service and task using cloudformation but it gives an error: Embedded stack arn:aws:cloudformation:us-east-2:0212657325299:stack/Root-Cluster-153O1DKDIKGLV/f1123c5c-d1f9-11ea-1216-2a3e4111fce2 was not successfully created: The following resource(s) failed to create: [Myservice, LoadBalancerListener]. I have created a root stack which runs the vpc stack and Cluster stack. This error occurs when running the Cluster stack. I think the error is in the Load balancer and role in Myservice but I am unable to figure the solution. Any help would be appreciated.

---
AWSTemplateFormatVersion: 2010-09-09 
Parameters:
    SubnetA:
      Type: String
    SubnetB:
      Type: String
    VpcID:
      Type: String
Resources:
    Albsg:
        Type: AWS::EC2::SecurityGroup
        Properties:
            GroupName: alb-sg
            VpcId: !Ref VpcID
            SecurityGroupIngress:
                - IpProtocol: tcp
                  FromPort: 22
                  ToPort: 22
                  CidrIp: 0.0.0.0/0
                  Description: For traffic from Internet
                - IpProtocol: tcp
                  FromPort: 80
                  ToPort: 80
                  CidrIp: 0.0.0.0/0
                  Description: For traffic from Internet
            GroupDescription: Security Group for demo server
    Alb:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties: 
            IpAddressType: ipv4
            Name: Alb
            Scheme: internet-facing
            SecurityGroups: 
                - !Ref Albsg
            Subnets:
                - Ref: "SubnetA"
                - Ref: "SubnetB"
            Type: application
    DefaultTargetGroup:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
        Properties:
            Name: alb-tg
            VpcId: !Ref VpcID
            Port: 80
            Protocol: HTTP
    LoadBalancerListener:
        Type: AWS::ElasticLoadBalancingV2::Listener
        Properties:
            LoadBalancerArn: !Ref Alb
            Port: 80
            Protocol: HTTP
            DefaultActions:
                - Type: forward
                  TargetGroupArn: !Ref DefaultTargetGroup 
    MyCluster:
        Type: AWS::ECS::Cluster
        Properties: 
            ClusterName: Flask-redis
    Myservice:
        Type: AWS::ECS::Service
        Properties: 
            Cluster: !Ref MyCluster        
            DeploymentController:   
                Type: ECS
            DesiredCount: 2
            LaunchType: EC2
            LoadBalancers: 
                - !Ref Alb
            # Role: String
            SchedulingStrategy: REPLICA
            ServiceName: Python-service
            TaskDefinition: !Ref Task
    Task:
        Type: AWS::ECS::TaskDefinition
        Properties:
            Family: redis-python 
            ContainerDefinitions: 
              - Essential: true
                Image: redis:latest
                Name: redis            
            Cpu: .5 vCPU
            # ExecutionRoleArn: !Ref Role
            Memory: 0.5 GB
            NetworkMode: bridge
            RequiresCompatibilities:
                - EC2---
AWSTemplateFormatVersion: 2010-09-09 
Parameters:
    SubnetA:
      Type: String
    SubnetB:
      Type: String
    VpcID:
      Type: String
Resources:
    Albsg:
        Type: AWS::EC2::SecurityGroup
        Properties:
            GroupName: alb-sg
            VpcId: !Ref VpcID
            SecurityGroupIngress:
                - IpProtocol: tcp
                  FromPort: 22
                  ToPort: 22
                  CidrIp: 0.0.0.0/0
                  Description: For traffic from Internet
                - IpProtocol: tcp
                  FromPort: 80
                  ToPort: 80
                  CidrIp: 0.0.0.0/0
                  Description: For traffic from Internet
            GroupDescription: Security Group for demo server
    Alb:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties: 
            IpAddressType: ipv4
            Name: Alb
            Scheme: internet-facing
            SecurityGroups: 
                - !Ref Albsg
            Subnets:
                - Ref: "SubnetA"
                - Ref: "SubnetB"
            Type: application
    DefaultTargetGroup:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
        Properties:
            Name: alb-tg
            VpcId: !Ref VpcID
            Port: 80
            Protocol: HTTP
    LoadBalancerListener:
        Type: AWS::ElasticLoadBalancingV2::Listener
        Properties:
            LoadBalancerArn: !Ref Alb
            Port: 80
            Protocol: HTTP
            DefaultActions:
                - Type: forward
                  TargetGroupArn: !Ref DefaultTargetGroup 
    MyCluster:
        Type: AWS::ECS::Cluster
        Properties: 
            ClusterName: Flask-redis
    Myservice:
        Type: AWS::ECS::Service
        Properties: 
            Cluster: !Ref MyCluster        
            DeploymentController:   
                Type: ECS
            DesiredCount: 2
            LaunchType: EC2
            LoadBalancers: 
                - !Ref Alb
            # Role: String
            SchedulingStrategy: REPLICA
            ServiceName: Python-service
            TaskDefinition: !Ref Task
    Task:
        Type: AWS::ECS::TaskDefinition
        Properties:
            Family: redis-python 
            ContainerDefinitions: 
              - Essential: true
                Image: redis:latest
                Name: redis            
            Cpu: .5 vCPU
            # ExecutionRoleArn: !Ref Role
            Memory: 0.5 GB
            NetworkMode: bridge
            RequiresCompatibilities:
                - EC2

Solution

  • The AWS::ECS::Service LoadBalancer is an object. There were also other issues:

    • Missing DependsOn
    • Missing port on container

    I used the template in us-east-1 using default VPC. The template will still not work as there are no container instances. But at least your original issue should be addressed.

    ---
    AWSTemplateFormatVersion: 2010-09-09 
    Parameters:
        SubnetA:
          Default: subnet-00afd36c5eb1d367b
          Type: String
        SubnetB:
          Default: subnet-0573cd428fe807ebc
          Type: String
        VpcID:
          Default: vpc-040d4c42ee5a159fc  
          Type: String
    Resources:
        Albsg:
            Type: AWS::EC2::SecurityGroup
            Properties:
                GroupName: alb-sg
                VpcId: !Ref VpcID
                SecurityGroupIngress:
                    - IpProtocol: tcp
                      FromPort: 22
                      ToPort: 22
                      CidrIp: 0.0.0.0/0
                      Description: For traffic from Internet
                    - IpProtocol: tcp
                      FromPort: 80
                      ToPort: 80
                      CidrIp: 0.0.0.0/0
                      Description: For traffic from Internet
                GroupDescription: Security Group for demo server
    
        Alb:
            Type: AWS::ElasticLoadBalancingV2::LoadBalancer
            Properties: 
                IpAddressType: ipv4
                Name: Alb
                Scheme: internet-facing
                SecurityGroups: 
                    - !Ref Albsg
                Subnets:
                    - Ref: "SubnetA"
                    - Ref: "SubnetB"
                Type: application
    
        DefaultTargetGroup:
            Type: AWS::ElasticLoadBalancingV2::TargetGroup
            Properties:
                Name: alb-tg
                VpcId: !Ref VpcID
                Port: 5000
                Protocol: HTTP
    
        LoadBalancerListener:
            Type: AWS::ElasticLoadBalancingV2::Listener
            Properties:
                LoadBalancerArn: !Ref Alb
                Port: 80
                Protocol: HTTP
                DefaultActions:
                    - Type: forward
                      TargetGroupArn: !Ref DefaultTargetGroup 
    
        MyCluster:
            Type: AWS::ECS::Cluster
            Properties: 
                ClusterName: Flask-redis
    
        Myservice:
            Type: AWS::ECS::Service
            DependsOn: LoadBalancerListener  
            Properties: 
                Cluster: !Ref MyCluster        
                DeploymentController:   
                    Type: ECS
                DesiredCount: 2
                LaunchType: EC2
                LoadBalancers: 
                    -  ContainerName: redis
                       ContainerPort: 5000
                       TargetGroupArn: !Ref DefaultTargetGroup 
                # Role: String
                SchedulingStrategy: REPLICA
                ServiceName: Python-service
                TaskDefinition: !Ref Task
    
        Task:
            Type: AWS::ECS::TaskDefinition
            Properties:
                Family: redis-python 
                ContainerDefinitions: 
                  - Essential: true
                    Image: redis:latest
                    Name: redis
                    PortMappings:
                      - ContainerPort: 5000
                        #HostPort: Integer
                        #Protocol: tcp
                Cpu: .5 vCPU
                # ExecutionRoleArn: !Ref Role
                Memory: 0.5 GB
                NetworkMode: bridge
                RequiresCompatibilities:
                    - EC2