AWS ECS: Severe bug in ECS Service Cloudformation

2019-07-29 23:51发布

问题:

Trying to create an ECS service using cloudformation via the following manifest that has the appropriate imports

 UIService:
        Type: AWS::ECS::Service
        Properties:
          Cluster: !ImportValue ECSClusterName
          DesiredCount: 1
          LaunchType: EC2
          LoadBalancers:
            - ContainerName: !ImportValue UIContainerName
              ContainerPort: '80'
              TargetGroupArn: !ImportValue UITGArn
          ServiceName: ui-service
          ServiceRegistries:
           - RegistryArn: arn:aws:servicediscovery:eu-west-1:944094092130:service/srv-oIclu40KCKM3esez7
          TaskDefinition: !ImportValue UITaskArn

This fails with the following message:

When specifying 'host' or 'bridge' for networkMode, values for 'containerName' and 'containerPort' must be specified from the task definition.

However when I add the wanted values (in the serviceregistry property where it makes me assume they are needed ?)

  UIService:
    Type: AWS::ECS::Service
    Properties:
      Cluster: !ImportValue ECSClusterName
      DesiredCount: 1
      LaunchType: EC2
      LoadBalancers:
        - ContainerName: !ImportValue UIContainerName
          ContainerPort: '80'
          TargetGroupArn: !ImportValue UITGArn
      ServiceName: ui-service
      ServiceRegistries:
       - RegistryArn: arn:aws:servicediscovery:eu-west-1:944094092130:service/srv-oIclu40KCKM3esez7
         ContainerName: !ImportValue UIContainerName
         ContainerPort: '80'
      TaskDefinition: !ImportValue UITaskArn

... I get the following failure:

Encountered unsupported property ContainerName

回答1:

This is a bug from AWS, in which the cloudformation is not supporting containerName and containerPort attribute currently. Here is the official docs for ServiceRegistry, and here is the docs for cloudformation. If I am not wrong, when service discovery was released, it only supported awsvpc networking mode and bridge/host was added later. It explains why we are having such discrepancy.

For the time being, you can create the basic ECS cloudformation, and update accordingly using CLI/API/SDK, or you can wait for sometimes so that AWS team will add support for the same.



回答2:

As per latest feature (introduced today), now you can specific the containerName and containerPort in ServiceRegistry.

I have quickly tested and it seems working just fine.