Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ECS restart Service with the same task definition and image with no downtime

I am trying to restart an AWS service (basically stop and start all tasks within the service) without making any changes to the task definition.

The reason for this is because the image has the latest tag attached with every build.

I have tried stopping all tasks and having the services recreate them but this means that there is some temporarily unavailable error when the services are restarting in my instances (2).

What is the best way to handle this? Say, A blue-green deployment strategy so that there is no downtime?

This is what I have currently. It'shortcomings is that my app will be down for a couple of seconds as the service's tasks are being rebuilt after deleting them.

configure_aws_cli(){     aws --version     aws configure set default.region us-east-1     aws configure set default.output json }  start_tasks() {     start_task=$(aws ecs start-task --cluster $CLUSTER --task-definition $DEFINITION --container-instances $EC2_INSTANCE --group $SERVICE_GROUP --started-by $SERVICE_ID)     echo "$start_task" }  stop_running_tasks() {     tasks=$(aws ecs list-tasks --cluster $CLUSTER --service $SERVICE | $JQ ".taskArns | . []");     tasks=( $tasks )     for task in "${tasks[@]}"     do         [[ ! -z "$task" ]] && stop_task=$(aws ecs stop-task --cluster $CLUSTER --task "$task")     done }  push_ecr_image(){     echo "Push built image to ECR"     eval $(aws ecr get-login --region us-east-1)     docker push $AWS_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/repository:$TAG }  configure_aws_cli push_ecr_image stop_running_tasks start_tasks 
like image 985
John Kariuki Avatar asked Mar 11 '17 12:03

John Kariuki


People also ask

Can we restart ECS service?

Go to Clusters. Click on the cluster that your service is in. Click on the service you'd like to restart. Click Update.

Can an ECS service have multiple task definitions?

Your application can span multiple task definitions. You can do this by combining related containers into their own task definitions, each representing a single component. For more information, see Application architecture.

How many containers can be defined in a task definition in ECS?

The task definition is a text file, in JSON format, that describes one or more containers, up to a maximum of ten, that form your application.


1 Answers

Use update-service and the --force-new-deployment flag:

aws ecs update-service --force-new-deployment --service my-service --cluster cluster-name 
like image 194
Ben Whaley Avatar answered Sep 20 '22 19:09

Ben Whaley