AWS ECS - 使用 Boto3 更新任务定义

Flo*_*Flo 3 python amazon-web-services amazon-ecs boto3

使用 boto3,我们可以创建一个新的任务定义:

client = boto3.client('ecs')
client.register_task_definition(...)
Run Code Online (Sandbox Code Playgroud)

我们如何更新现有的任务定义?难道只是又一个电话变了,姓氏也一样了?

Tre*_*ths 9

如上所述,您必须使用 register_task_definition 创建任务定义的新修订版。非常烦人的是,即使您可能只想更改 1,您也必须再次传入所有参数。这将创建一个新的修订版。

这非常烦人,因为这是标准 ECS 部署的工作流程。即使用更新的图像标签创建新的任务定义修订版。为什么没有一些内置功能来实现这一点......

我所做的只是调用describe_task_definition,更新响应字典以修改您想要的内容,删除不能在kwargs中用于调用register_task_definition的任何键,然后调用register_task_definition并传入该字典。对我来说效果很好。:

existing_task_def_response = ecs_client.describe_task_definition(
    taskDefinition=ecs_task_definition_name,
)
new_task_definition = existing_task_def_response['taskDefinition']
#edit the image tag here
new_task_definition['containerDefinitions'][0]['image'] = yournewimagetagforexample

#drop all the keys from the dict that you can't use as kwargs in the next call. could be explicit here and map things
remove_args=['compatibilities', 'registeredAt', 'registeredBy', 'status', 'revision', 'taskDefinitionArn', 'requiresAttributes' ]
for arg in remove_args:
    new_task_definition.pop(arg)

reg_task_def_response = ecs_client.register_task_definition(
**new_task_definition
)
Run Code Online (Sandbox Code Playgroud)