AWS ECS – Using Boto3 to update a task definition
Question:
Using boto3, we can create a new task definition:
client = boto3.client('ecs')
client.register_task_definition(...)
How do we update an existing task definition? Is it just another call with changes and the same family name?
Answers:
update an existing task definition
You can’t do this. You have to create a new revision of an existing task definition. Then you will also have to update your ECS service to use the new task revision. Running register_task_definition
again should automatically create new revision for you.
As mentioned above you have to create a new revision for the task definition using register_task_definition. Highly Annoyingly, you have to pass in all the parameters again even though you probably only want to change 1. This will create a new revision.
This is super annoying as this is the workflow for a standard ECS deployment. ie create a new task definition revision with updated tag for image. Why would there not be some built in functionality for this…
What i have done is just call describe_task_definition, update the response dict to modify what you want, drop any keys that can’t be used in kwargs for calling register_task_definition and then call register_task_definition and pass in that dict. Works fine for me. :
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
)
Using boto3, we can create a new task definition:
client = boto3.client('ecs')
client.register_task_definition(...)
How do we update an existing task definition? Is it just another call with changes and the same family name?
update an existing task definition
You can’t do this. You have to create a new revision of an existing task definition. Then you will also have to update your ECS service to use the new task revision. Running register_task_definition
again should automatically create new revision for you.
As mentioned above you have to create a new revision for the task definition using register_task_definition. Highly Annoyingly, you have to pass in all the parameters again even though you probably only want to change 1. This will create a new revision.
This is super annoying as this is the workflow for a standard ECS deployment. ie create a new task definition revision with updated tag for image. Why would there not be some built in functionality for this…
What i have done is just call describe_task_definition, update the response dict to modify what you want, drop any keys that can’t be used in kwargs for calling register_task_definition and then call register_task_definition and pass in that dict. Works fine for me. :
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
)