3

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?

Shams Larbi
  • 151
  • 2
  • 5

3 Answers3

1

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

Asri Badlah
  • 1,949
  • 1
  • 9
  • 20
  • Thank you, but I still need to manually execute this command line here. I actually looking at automating this. – Shams Larbi Apr 25 '21 at 20:54
  • For reference, if you are trying to manually update the service to use the __latest task definition revision__ you will need to add the task-definition family name: `aws ecs update-service --cluster cluster-name --service service-name --task-definition task-definition-family-name --force-new-deployment` – hmartos Feb 09 '23 at 10:04
1

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.

mreferre
  • 5,464
  • 3
  • 22
  • 29
  • I want to change the container environment { "name": "DB_USE_SSL", "value": "true" }, .....any idea how to do it using aws cli – Ashish Karpe Nov 17 '21 at 04:31
  • the variables are a property of the task and task definition (not of the service). I thought you had that part covered with the `have updated my task definition many times` part. You basically need to add those variables to the task def when you run the [aws ecs register-task-definition](https://docs.aws.amazon.com/cli/latest/reference/ecs/register-task-definition.html) command. – mreferre Nov 17 '21 at 08:15
1

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.

smee
  • 231
  • 1
  • 5