AWS ECS:ARN中的服务无效(服务:AmazonECS; ...)

时间:2019-03-20 04:25:36

标签: amazon-web-services amazon-cloudformation amazon-ecs aws-fargate

尝试使用cloudformation创建ECS服务(在Fargate上),但出现错误:

  

ARN中的服务无效(服务:AmazonECS;状态代码:400;错误   代码:InvalidParameterException;请求ID:xxx)。

根据错误消息,似乎有些ARN是错误的,但是我没有找到原因,我检查了IAM角色的ARN及其确定。另一个ARN与!Ref函数一起传递(因此不会出现拼写错误)

除了“服务”资源(ECS服务)以外,所有资源(包括所有其他嵌套模板,vpc,集群,alb等)均已创建。

下面是使用的模板(嵌套模板)。所有参数均正确(从根模板传递)。参数TaskExecutionRole和ServiceRole是ECS向导创建的IAM角色的ARN:

Description: >
  Deploys xxx ECS service, with load balancer listener rule,
  target group, task definition, service definition and auto scaling

Parameters:
  EnvironmentName:
    Description: An environment name that will be prefixed to resource names
    Type: String
  EnvironmentType:
    Description: See master template
    Type: String
  VpcId:
    Type: String
  PublicSubnet1:
    Type: String
  PublicSubnet2:
    Type: String
  ALBListener:
    Description: ALB listener
    Type: String
  Cluster:
    Description: ECS Cluster
    Type: String
  TaskExecutionRole:
    Description: See master template
    Type: String
  ServiceRole:
    Description: See master template
    Type: String
  ServiceName:
    Description: Service name (used as a variable)
    Type: String
    Default: xxx
  Cpu:
    Description: Task size (CPU)
    Type: String
  Memory:
    Description: Task size (memory)
    Type: String

Conditions:
  HasHttps: !Equals [!Ref EnvironmentType, production]
  HasNotHttps: !Not [!Equals [!Ref EnvironmentType, production]]

Resources:
  ServiceTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: !Sub '${EnvironmentName}-${ServiceName}'
      VpcId: !Ref VpcId
      TargetType: ip
      Port: 80
      Protocol: HTTP

  AlbListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      Actions:
      - Type: forward
        TargetGroupArn: !Ref ServiceTargetGroup
      Conditions:
      - Field: host-header
        Values: [www.mydomain.com] # test
      ListenerArn: !Ref ALBListener
      Priority: 1

  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: !Sub '${EnvironmentName}-${ServiceName}-Task'
      ContainerDefinitions:
        - Name: !Ref ServiceName
          Image: nginx
          PortMappings:
          - ContainerPort: 80
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref EnvironmentName
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: !Ref ServiceName
      NetworkMode: awsvpc
      RequiresCompatibilities: [FARGATE]
      Cpu: !Ref Cpu
      Memory: !Ref Memory
      ExecutionRoleArn: !Ref TaskExecutionRole

  Service:
    Type: AWS::ECS::Service
    DependsOn: TaskDefinition
    Properties:
      Cluster: !Ref Cluster
      ServiceName: !Ref ServiceName
      TaskDefinition: !Ref TaskDefinition
      LaunchType: FARGATE
      DesiredCount: 1
      LoadBalancers:
      - ContainerName: !Ref ServiceName
        ContainerPort: 80
        TargetGroupArn: !Ref ServiceTargetGroup
      NetworkConfiguration:
        AwsvpcConfiguration:
          AssignPublicIp: ENABLED
          Subnets:
            - !Ref PublicSubnet1
            - !Ref PublicSubnet2
      Role: !Ref ServiceRole

我为此花了几个小时而无法解决,我在文档中回顾了很多,但如果有人知道如何提供帮助,则什么也没有。

谢谢!

1 个答案:

答案 0 :(得分:2)

错误信息令人困惑,因为它没有说明哪个参数是错误的。 Amazon API 需要多个参数中的资源 ARN,包括 ClusterTaskDefinitionTargetGroup。当这些参数之一错误时会发生错误。请仔细检查这些参数并确保它们是有效的 ARN。

我遇到了完全相同的错误,就我而言,我犯了一个错误并提供了错误的 Cluster 值。

我在这里发布答案是因为这是此错误消息的第一个搜索结果,但没有答案。