Bootstrap FreeKB - Amazon Web Services (AWS) Elastic Container Service (ECS) - Resolve "No Container Instances were found in your cluster"
Amazon Web Services (AWS) Elastic Container Service (ECS) - Resolve "No Container Instances were found in your cluster"


Let's say something like this is being returned when attempting to deploy a Task or Service in your Elastic Container Services (ECS) Cluster.

No Container Instances were found in your cluster

 

I find this typically happens when attempting to deploy a service that will run on an EC2 instance. 

{
    "cluster": "my-ecs-cluster",
    "serviceName": "my-nginx-service",
    "launchType": "EC2",
    "platformVersion": "LATEST",
    "taskDefinition": "nginx:1",
    "networkConfiguration": {
      "awsvpcConfiguration": {
        "subnets": [
          "subnet-12345678912345678"
        ],
        "securityGroups": [
          "sg-12345678912345678"
        ],
        "assignPublicIp": "ENABLED"
      }
    },
    "desiredCount": 1
}

 

When creating the EC2 instance, you will need to use one of the ECS Optimized image, almost always the latest ECS Optimized image. You can get the ID of the latest ECS Optimized image using the aws ec2 describe-images command.

~]$ aws ec2 describe-images | grep -i amzn-ami | grep -i ecs | grep -i optimized
            "ImageLocation": "amazon/amzn-ami-2018.03.20230530-amazon-ecs-optimized",
            "Name": "amzn-ami-2018.03.20230530-amazon-ecs-optimized",

 

Then, using the name of the ECS Optimized image, get the ID of the image.

~]$ aws ec2 describe-images --filters "Name=name,Values=amzn-ami-2018.03.20230530-amazon-ecs-optimized" | grep ImageId
            "ImageId": "ami-00e3e84b6c67d2167",

 

Additionally, when creating the EC2 instance, an IAM instance profile that contains a role that contains the AmazonEC2ContainerServiceforEC2Role policy will need to be used. When creating a role you will need to include JSON. For example, let's say my.json contains the following

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

 

The aws iam create-role command can be used to create an IAM Role using the JSON file.

aws iam create-role --role-name my-role --assume-role-policy-document file://my.json

 

And then use the aws iam create-instance-profile command to create a profile.

aws iam create-instance-profile --instance-profile-name my-profile

 

And then use the aws iam add-role-to-instance-profile command to add the role to the profile.

aws iam add-role-to-instance-profile --role-name my-role --instance-profile-name my-profile

 

And you can now create the EC2 instance using the aws ec2 run-instances command.

  • Using the ID of the ECS Optimized image
  • Using your IAM Profile
  • Using --user-data that includes the name of your ECS Cluster
aws ec2 run-instances 
--image-id ami-00e3e84b6c67d2167 \
--count 1 \
--key-name default \
--security-group-ids sg-0123456789abcdefg \
--subnet-id subnet-0123456789abcdefg \
--iam-instance-profile my-profile \
--user-data <<EOF #!/bin/bash echo ECS_CLUSTER=<the name of your ECS cluster> >> /etc/ecs/ecs.config EOF

 

And the EC2 instance should be listed in Container Instances in the ECS console.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter ef3276 in the box below so that we can be sure you are a human.