首页 > 解决方案 > 无法从 boto3 注册 ECS 任务

问题描述

我正在尝试创建一个可以通过任何 AWS 帐户运行的基本云基础设施。我正在使用 python boto3 模块来创建集群、任务定义和服务,这些都是从我的代码中成功创建的。但是,该任务未从我的服务运行,当我检查事件时,我可以看到错误“服务 my_service 无法放置任务,因为没有容器实例满足其所有要求。原因:在您的集群中未找到任何容器实例'

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)

我希望这能够在服务生成的任务中运行我的 docker 映像。AWS 账户没有任何现有的 EC2 实例,我想运行一个从该代码创建 EC2 实例的任务(无需从 EC2 实例内部更改任何内容)。如何运行任务?

标签: pythonamazon-web-servicesboto3

解决方案


推荐阅读