program story

작업 실행시 AWS ECS 오류 : 클러스터에 컨테이너 인스턴스가 없습니다.

inputbox 2020. 9. 7. 08:05
반응형

작업 실행시 AWS ECS 오류 : 클러스터에 컨테이너 인스턴스가 없습니다.


dockerAWS사용하여 컨테이너 이미지 를 배포하려고 ECS하지만 EC2 인스턴스가 생성되지 않습니다. 다음 오류가 발생하는 이유에 대한 설명을 찾기 위해 인터넷을 검색했습니다.

"RunTask 작업을 호출 할 때 클라이언트 오류 (InvalidParameterException)가 발생했습니다. 클러스터에서 컨테이너 인스턴스를 찾을 수 없습니다."

내 단계는 다음과 같습니다.

1. Ubuntu에서 Amazon ECS repo로 Docker 이미지를 푸시했습니다.

2. ECS 작업 정의 등록 :

aws ecs register-task-definition --cli-input-json file://path/to/my-task.json 

3. 작업을 실행했습니다.

aws ecs run-task --task-definition my-task

그러나 그것은 실패합니다.

내 임무는 다음과 같습니다.

{
  "family": "my-task",
  "containerDefinitions": [
    {
        "environment": [],
        "name": "my-container",
        "image": "my-namespace/my-image",
        "cpu": 10,
        "memory": 500,
        "portMappings": [
            {
                "containerPort": 8080,
                "hostPort": 80
            }
        ],
        "entryPoint": [
            "java",
            "-jar",
            "my-jar.jar"
        ],
        "essential": true
    }
  ]
}

I have also tried using the management console to configure a cluster and services, yet I get the same error. How do I configure the cluster to have ec2 instances, and what kind of container instances do I need to use? I thought this whole process was to create the EC2 instances to begin with!!


I figured this out after a few more hours of investigating. Amazon, if you are listening, you should state this somewhere in your management console when creating a cluster or adding instances to the cluster:

"Before you can add ECS instances to a cluster you must first go to the EC2 Management Console and create ecs-optimized instances with an IAM role that has the AmazonEC2ContainerServiceforEC2Role policy attached"

Here is the rigmarole:

1. Go to your EC2 Dashboard, and click the Launch Instance button.

2. Under Community AMIs, Search for ecs-optimized, and select the one that best fits your project needs. Any will work. Click next.

3. When you get to Configure Instance Details, click on the create new IAM role link and create a new role called ecsInstanceRole.

4. Attach the AmazonEC2ContainerServiceforEC2Role policy to that role.

5. Then, finish configuring your ECS Instance.
NOTE: If you are creating a web server you will want to create a securityGroup to allow access to port 80.

After a few minutes, when the instance is initialized and running you can refresh the ECS Instances tab you are trying to add instances too.


Currently, the Amazon AWS web interface can automatically create instances with the correct AMI and the correct name so it'll register to the correct cluster.

Even though all instances were created by Amazon with the correct settings, my instances wouldn't register. On the Amazon AWS forums I found a clue. It turns out that your clusters need internet access and if your private VPC does not have an internet gateway, the clusters won't be able to connect.

The fix

In the VPC dashboard you should create a new Internet Gateway and connect it to the VPC used by the cluster. Once attached you must update (or create) the route table for the VPC and add as last line

0.0.0.0/0 igw-24b16740  

Where igw-24b16740 is the name of your freshly created internet gateway.


Other suggested checks

  1. Selecting the suggested AMI which was specified for the given region solved my problem.

    To find out the AMI - check Launching an Amazon ECS Container Instance.

  2. By default all the ec2 instances are added to default cluster . So the name of the cluster also matters.

See point 10 at Launching an Amazon ECS Container Instance.

More information available in this thread.


I ran into this issue when using Fargate. I fixed it when I explicitly defined launchType="FARGATE" when calling run_task.


If you have come across this issue after creating the cluster

Go the ECS instance in the EC2 instances list and check the IAM role that you have assigned to the instance. You can identify the instances easily with the instance name starts with ECS Instance

enter image description here

After that click on the IAM role and it will direct you to the IAM console. Select the AmazonEC2ContainerServiceforEC2Role policy from the permission policy list and save the role.

Your instances will be available in the cluster shortly after you save it.


The real issue is lack of permission. As long as you create and assign a IAM Role with AmazonEC2ContainerServiceforEC2Role permission, the problem goes away.


Just in case someone else is blocked with this problem as I was... I've tried everything here and didn't work for me.

Besides what was said here regards the EC2 Instance Role, as commented here, in my case only worked if I still configured the EC2 Instance with simple information. Using the User Data an initial script like this:

#!/bin/bash
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=quarkus-ec2
EOF

Informing the related ECS Cluster Name created at this ecs config file, resolved my problem. Without this config, the ECS Agent Log at the EC2 Instance was showing an error that was not possible to connect to the ECS, doing this I've got the EC2 Instance visible to the ECS Cluster.

After doing this, I could get the EC2 Instance available for my EC2 Cluster: enter image description here

The AWS documentation said that this part is optional, but in my case, it didn't work without this "optional" configuration.


Another possible cause that I ran into was updating my ECS cluster AMI to an "Amazon Linux 2" AMI instead of an "Amazon Linux AMI", which caused my EC2 user_data launch script to not work.

참고URL : https://stackoverflow.com/questions/36523282/aws-ecs-error-when-running-task-no-container-instances-were-found-in-your-clust

반응형