AWS ECS Task definition update

7.6k views Asked by At

I'm setting up new services in ECS and have updated my task definition many times. When I do so, I need to edit the correspondent service and update the version to the latest.

Is there a way to automatically update an AWS service with the latest version of its correspondent task definition when it's updated?

3

There are 3 answers

2
mreferre On

I think you have a couple of options here. The first one is, depending on how you update the task def (manually? through a pipeline?) you could queue the force-new-deployment call for the service after the task update call.

If this is not possible (for reasons) and yet you need to automate this, you probably need to look at leveraging Event Bridge where you'd create a rule to intercept the update task definition api call you are making which would then invoke a Lambda that does the "force new deployment" on your behalf.

2
Asri Badlah On

To force ECS service to re-evaluated the task definition and pull the new container image. use

aws ecs update-service --cluster cluster_name --service service_name --force-new-deployment

0
smee On

The answer is yes, but it's a complicated process. The best way I know is to use CodePipeline for your build automation, and have your build step produce a new taskdef.json and a new appspec.yml file as output artifacts.

These are then inputs to the deploy step. Assuming you've set up a CodeDeploy Application, with a Deployment Group linked to a load balancer, you can then have CodeDeploy update your service to use the new task definition.

However, the key to making this work is to have the code that generates the new appspec.yml find the current revision number of the task definition and add 1 to it.

Without incrementing the revision, it will just stay on the old version of the task def even though it will create the new revision.

I've done this using the SDK, but I imagine you could do it with some cunning CLI scripts and jq. For example, to find the latest version of the taskdef:

aws ecs list-task-definitions --family-prefix <taskdef-family> --query 'taskDefinitionArns[-1]'

It's not too far from there to incrementing the revision number and building your new appspec.yml containing the 'next' revision of the taskdef.