使用 cloudformation 在 ECS 中配置应用程序负载均衡器时出错

Error while configuring Application loadbalancer in ECS using cloudformation

我正在使用 cloudformation 创建 ECS 集群、服务和任务,但出现错误: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].我创建了一个运行 vpc 堆栈和集群堆栈的根堆栈。 运行 Cluster 堆栈时会出现此错误。我认为错误出在负载均衡器和 Myservice 中的角色,但我无法找到解决方案。任何帮助将不胜感激。

---
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

AWS::ECS::Service LoadBalancer是一个对象。还有其他问题:

  • 失踪DependsOn
  • 容器上缺少端口

我在 us-east-1 中使用默认 VPC 中的模板。由于 没有容器实例 ,该模板仍然无法工作。但至少你原来的问题应该得到解决。

---
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