cloudformation ecs force new deployement

Viewed 1261

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

3 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.

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

The best I have been able to come up with is to change my healthcheck from curl -f to curl --fail and back to force a new deployment. Not ideal but it works.

I'd be interested in a better way, ideally built in to CloudFormation too.

Related