Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cloudformation ecs force new deployement

What is the equivalent of --force-new-deployment flag in cloudformation ? (aws ecs update-service --cluster --service --force-new-deployment)

In my cloudformation template, my docker image tag is latest. However I want to be able to force the redeployment if I replay the stack.

Thanks you

like image 709
Geoffrey Deremetz Avatar asked Sep 17 '25 01:09

Geoffrey Deremetz


2 Answers

Think of CloudFormation as just a provisioning service or an orchestrator.

Specifying an Image as repository-url/image:tag will definitely fetch the specified image with specific tag from your repo but only upon a stack operation. Once a stack operation is finished, any changes to service will be depending on the next stack operation.

What you can do here is either having either

  • Cloudwatch Event based rule targeting a Lambda Function whenever there is an image upload to ECR.
  • Or in case of using some other repo than ECR, have a hook configured in a way that will invoke update-service --cluster --service --force-new-deployment ... command whenever there is a new image upload.

Considerations:

  • This can lead your stack being in DRIFTED status.
  • Just make sure to keep your stack's Image property's value in sync with the latest one running within service, whenever you plan to update the stack.
like image 189
hammad Avatar answered Sep 18 '25 15:09

hammad


Here's something I came up with to force a deploy after a stack update if needed. The service name is the same as the stack name, so if yours is different you will need to adjust accordingly.

START_TIME=`date +%s`
aws cloudformation deploy --stack-name $STACK_NAME [other options]
LAST_DEPLOY=`aws ecs describe-services --services $STACK_NAME --query "services[0].deployments[?status=='PRIMARY'].createdAt" --output text`
if [ "$START_TIME" -gt "${LAST_DEPLOY%.*}" ]; then aws ecs update-service --service $STACK_NAME --force-new-deployment; fi
like image 43
Nabrok Avatar answered Sep 18 '25 15:09

Nabrok