AWS ECS:ECSService CloudFormation 资源定义中的 LoadBalancers 属性 错误

AWS ECS: LoadBalancers property error in ECSService CloudFormation resource definition

正在尝试使用以下 Cloudformation 资源定义创建 ECS 服务:

  MyUIService:
    Type: AWS::ECS::Service
    Properties:
      Cluster: !ImportValue MyClusterName
      DesiredCount: 1
      LaunchType: EC2
      LoadBalancers:
        - !ImportValue ALBDemo
      Role: !Ref MyServiceRole
      ServiceName: my-ui-service
      ServiceRegistries:
       - arn:aws:servicediscovery:eu-west-1:2398942890842:service/srv-ohc098mdj55yesez7
      TaskDefinition: !ImportValue MyTaskArn

但是失败并出现以下错误:

Value of property LoadBalancers must be a list of objects

但是我正在定义一个列表(尽管只有一个元素)。

该列表包含 ALB 的 arn 的导出。

确切的语法是什么?

edit:这里是相关的documentation好像与错误不一致:

LoadBalancers

A list of load balancer objects to associate with the cluster. If you specify the Role property, LoadBalancers must be specified as well. For information about the number of load balancers that you can specify per service, see Service Load Balancing in the Amazon Elastic Container Service Developer Guide. Required: Conditional Type: List of Amazon Elastic Container Service Service LoadBalancers

我看到您从 AWS templates 复制了相同的模板。

MyUIService:
  Type: AWS::ECS::Service
  Properties:
    Cluster: !ImportValue MyClusterName
    DesiredCount: 1
    LaunchType: EC2
    LoadBalancers:
      - ContainerName: simple-app
        ContainerPort: '80'
        TargetGroupArn: !Ref 'ECSTG'
    Role: !Ref MyServiceRole
    ServiceName: my-ui-service
    ServiceRegistries:
     - arn:aws:servicediscovery:eu-west-1:2398942890842:service/srv-ohc098mdj55yesez7
    TaskDefinition: !ImportValue MyTaskArn

请注意,LoadBalancers 并不是真正直接引用负载均衡器。它引用目标组。鉴于命名,这很奇怪,但如果您通过 Web 控制台,您将得出相同的结论。

如果你看AWS documentation

ContainerName
The name of a container to use with the load balancer.

Required: Yes

Type: String


ContainerPort
The port number on the container to direct load balancer traffic to. Your container instances must allow ingress traffic on this port.

Required: Yes

Type: Integer

这些是必需的,但您永远无法通过导入负载均衡器来获得它们。

如果您考虑一下,通过引用目标组而不是负载均衡器,您可以为多个目标组共享同一个 ALB,这对成本有利。因此总而言之,引用目标群体是有意义的,但 属性 名称确实具有误导性。