sheepinwild
sheepinwild

Reputation: 541

AWS ECS unable to place a task because no container instance met all of its requirements

I'm using .NET Core WEBAPI and below Dockerfile

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DummyService.dll"]

In my cloudformation template, the ECS part looks like this

  dummyWebApiEcsTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
  Family: !Ref AWS::StackName
  TaskRoleArn: !GetAtt dummyWebApiIamRole.Arn
  ContainerDefinitions:
    - Name: !Ref AWS::StackName
      Image: MY IMAGE URL
      DnsSearchDomains:
        - !Join [".", [{"Fn::ImportValue": !Sub "${accountStackName}-${AWS::Region}-envName"}, "connected", !If [chinaPartition, "TEST", "CORP"], "cloud"]]
      LogConfiguration:
        LogDriver: splunk
        Options:
          splunk-token: {"Fn::ImportValue": !Sub "${splunkHECStackName}-${AWS::Region}-SplunkHECToken"}
          splunk-url: "http://splunk-forwarder:8088"
          splunk-insecureskipverify: True
          tag: !Ref AWS::StackName
          splunk-format: json
          splunk-source: !Ref AWS::StackName
          splunk-sourcetype: AWS:ECS
      EntryPoint: []
      PortMappings:
        - ContainerPort: 5000
      Command: []
      Cpu: 0
      Environment:
        - Name: BindAddress
          Value: http://0.0.0.0:5000
        - Name: MinLogLevel
          Value: !If [isProduction, "Information", "Debug"]
      Ulimits: []
      DnsServers: []
      MountPoints: []
      DockerSecurityOptions: []
      Memory: 512
      VolumesFrom: []
      Essential: true
      ExtraHosts: []
      ReadonlyRootFilesystem: false
      DockerLabels: {}
      Privileged: false

  dummyEcsService:
Type: AWS::ECS::Service
DependsOn:
  - dummyWebApiIamRole
  - dummyInternalAlb
  - dummyAlbTargetGroup
Properties:
  Cluster:
    Fn::ImportValue: !Sub "cld-core-ecs-${AWS::Region}-ECSCluster"
  DeploymentConfiguration:
    MaximumPercent: 200
    MinimumHealthyPercent: 50
  DesiredCount: 2
  LoadBalancers:
    - ContainerName: !Ref AWS::StackName
      ContainerPort: 5000
      TargetGroupArn: !Ref dummyAlbTargetGroup
  PlacementStrategies:
    - Type: spread
      Field: attribute:ecs.availability-zone
  TaskDefinition: !Ref dummyWebApiEcsTaskDefinition
  ServiceName: !Ref AWS::StackName
  Role: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS"

The deployment couldn't finish and I can see this error in the ECS Service Events tab

service cld-dummy-test was unable to place a task because no container instance met all of its requirements. Reason: No Container Instances were found in your cluster.

Upvotes: 8

Views: 30675

Answers (3)

alexkb
alexkb

Reputation: 3356

I was also having the same error, but I was using ecs-cli to create the cluster, tasks and service, so manually registering the EC2 instance to the cluster had already been done (as suggested by the sheepinwild's answer).

What solved the issue for us was making sure the IAM role assigned to the instance had the AWS managed policy AmazonEC2ContainerServiceforEC2Role. I only discovered this as we had another ECS instance running successfully that I compared against. If you're using ecs-cli, this is the role you pass like so ecs-cli up --instance-role HERE. Alternatively, you can also pass --capability-iam and that will create a new role with the correct policies and assign it to your instance for you. More info on the AWS KB for ecs-cli.

Upvotes: 0

Harsh Manvar
Harsh Manvar

Reputation: 30083

AWS ECS has two launch type config :

  • Fargate
  • Fargate + EC2

in both cases you can not access underlying resources.

so may possible cause of iusse in launch type configuration you are not able to spin up task otherwise from ecs dashboard you can choose launch type and also choose task defination.

Upvotes: 0

sheepinwild
sheepinwild

Reputation: 541

I eventually got this figured out. The error message below indicates that there's no EC2 in this cluster, and hence no container can be started. We are not using Fargate.

service cld-dummy-test was unable to place a task because no container instance met all of its requirements. Reason: No Container Instances were found in your cluster.

To register an EC2 to a cluster, you need to follow this AWS article. https://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_container_instance.html

Please be aware that the EC2 you start need to have below userdata in order for it to be registered.

#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config

Once the above is completed, you shouldn't see the error about "no container". However, if you are like me, having the splunk logging section in the template. You will have a different issue which says something like no container can be used for the task because it is missing an attribute. This is quite a vague message and the attribute can be anything that is listed at the bottom of your task definition page.

In my case it was the splunk logging. The splunk driver needs to be added to the EC2 instance. Since I later found out that we don't need splunk anymore so I removed the splunk section. But if you want to do that, you probably need to add the below line to your userdata.

ECS_AVAILABLE_LOGGING_DRIVERS=["splunk","awslogs"]

I hope this helps someone.

Upvotes: 20

Related Questions