title: AWS ECS: Invalid service in ARN (Service: AmazonECS; ...)
body: <p>Trying to create a ECS Service (on Fargate) with cloudformation but got error:</p>
  
  <blockquote>
    <p>Invalid service in ARN (Service: AmazonECS; Status Code: 400; Error
    Code: InvalidParameterException; Request ID: xxx).</p>
  </blockquote>
  
  <p>According to error message seems some ARN is wrong, but I didn't find the reason, I checked ARN of IAM roles and its ok. The other ARN are passed with !Ref function (so not a typo error)</p>
  
  <p>All Resources (including from all others nested templates, vpc, cluster, alb etc) are created, except the "Service" resouce (the ECS service).</p>
  
  <p>Below is the template used (nested template). All parameters are ok (passed from root template). Parameters TaskExecutionRole and ServiceRole are ARNs from IAM roles created by ECS wizard:</p>
  
  <pre><code>Description: &gt;
    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
  </code></pre>
  
  <p>I lost a few hours in this and could not solve it, I reviewed a lot in the documentation but nothing, if someone knows how to help.</p>
  
  <p>Thanks!</p>