2

I am trying to create a basic cloud infrastructure which can run through any AWS account. I am using python boto3 module to create a cluster,task definition and a service, which are created successfully from my code. However the task in not running from my service, when I checked the Events I can see error 'service my_service was unable to place a task because no container instance met all of its requirements. Reason: No Container Instances were found in your cluster'.

import boto3
client = boto3.client('ecs')
cluster_response = client.create_cluster(
    clusterName='my_cluster',
)
print(cluster_response)

taskdef_response = client.register_task_definition(
    family = 'my_taskdef',
    containerDefinitions=[
        {
            'name': 'my_taskdef',
            'image': '****/sample:latest'
        }
    ],
    memory='256',
    cpu='1024',
    taskRoleArn='ecsTaskExecutionRole'
)
print(taskdef_response)
service_response = client.create_service(
    cluster='my_cluster',
    serviceName='my_service',
    taskDefinition='my_taskdef',
    desiredCount=1,
    launchType='EC2'
)
print(service_response)

I am expecting this to run my docker image in the task spawned by the service. The AWS account does not have any existing EC2 instance and I want to run a task that creates EC2 instance from this code(without the need of changing anything from inside EC2 instance). How can I run the task?

bot
  • 1,293
  • 3
  • 17
  • 34
  • please share if you found the solution ? – Bikesh M Jun 29 '20 at 12:49
  • @BikeshM I followed a different approach without ECS. But this error was coming because it was missing [this](https://stackoverflow.com/questions/36523282/aws-ecs-error-when-running-task-no-container-instances-were-found-in-your-cluster) part. The code also requires a new `ecs-optimized` instance with `AmazonEC2ContainerServiceforEC2Role` policy attached. – bot Jun 29 '20 at 15:21

0 Answers0