Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ECS: Invalid service in ARN (Service: AmazonECS; ...)

Trying to create a ECS Service (on Fargate) with cloudformation but got error:

Invalid service in ARN (Service: AmazonECS; Status Code: 400; Error Code: InvalidParameterException; Request ID: xxx).

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)

All Resources (including from all others nested templates, vpc, cluster, alb etc) are created, except the "Service" resouce (the ECS service).

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:

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

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.

Thanks!

like image 996
Rodrigo Pires Avatar asked Oct 20 '25 16:10

Rodrigo Pires


1 Answers

The error message is confusing because it does not explain which parameter is wrong. Amazon API expects resource ARNs in several parameters including Cluster, TaskDefinition and TargetGroup. The error happens when one of these parameters are wrong. Please check carefully these parameters and make sure they are valid ARNs.

I had exactly the same error and in my case I made a mistake and provided wrong Cluster value.

And I am posting an answer here because this was the first search result for this error message and it had no answer.

like image 118
Victor Smirnov Avatar answered Oct 23 '25 06:10

Victor Smirnov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!