Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ECS: Force redeployment on new latest image in ECR

I know that there are already countless questions in this direction, but unfortunately I was not able to find the right answer yet. If a post already exists, please just share the link here.

I have several gitlab CI / CD pipelines. The first pipeline uses Terraform to build the complete infrastructure for an ECS cluster based on Fargate. The second / third pipeline creates nightly builds of the frontend and the backend and pushes the Docker Image with the tag "latest" into the ECR of the (staging) AWS account.

What I now want to achieve is that the corresponding ECS tasks are redeloyed so that the latest Docker images are used. I actually thought that there is a way to do this via CloudWatch Events or whatsoever, but I don't find a really good starting point here. A workaround would be to install the AWS CLI in the CI / CD pipeline and then do a service update with "force new deployment". But that doesn't seem very elegant to me. Is there any better way here?

Conditions:

  • The solution must be fully automated (either in AWS or in gitlab CI / CD)
  • Switching to AWS CodePipeline is out of discussion
  • Ideally as close as possible to AWS standards. I would like to avoid extensive lambda functions that perform numerous actions due to their maintainability.

Thanks a lot!

like image 385
Stefan Avatar asked Nov 26 '25 13:11

Stefan


2 Answers

Ok, for everybody who is interested in an answer. I solved it that way: I execute the following AWS CLI command in the CICD pipeline

aws ecs update-service --cluster <<cluster-name>> --service <<service-name>> --force-new-deployment --region <<region>>

Not the solution I was looking for but it works.

like image 77
Stefan Avatar answered Nov 29 '25 19:11

Stefan


As a general comment it is not recommended to always push the same container tag because then rolling back to a previous version in case of failure becomes really difficult.

One suitable option would be to use git tags. Let's say you are deploying version v0.0.1 You can create a file app-version.tf which will contain the variable backend-version = v0.0.1 that you can reference on the task definition of the ecs service. Same thing can be done for the container creation using git describe.

So, you get a new task definition for every git tag and the possibility of rolling back just by changing a value in the terraform configuration.

like image 23
jackops Avatar answered Nov 29 '25 18:11

jackops



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!