Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart containers in AWS ECS?

I have provided application configuration via consul's key-value store to the application containers running in ECS services.

The application reads its configuration from consul only once on start up.

When I need to change the configuration, how should I go about restarting the containers so that the application configuration is refreshed?

I am hoping to do this programmatically via the aws cli.

like image 673
Nick Avatar asked Apr 08 '17 16:04

Nick


People also ask

How do you restart ECS?

To reboot an instance using the consoleOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances. Select the instance and choose Instance state, Reboot instance.

How do I know if ECS container is running?

You can use the docker ps command on your container instance to list the running containers. In the below example, only the Amazon ECS container agent is running. For more information, see docker ps in the Docker documentation.


4 Answers

You don't restart containers. You can however stop the individual tasks, and ECS will respawn another instance of your task somewhere on the cluster.

like image 93
Mitch Dempsey Avatar answered Oct 18 '22 20:10

Mitch Dempsey


Update:

As @Aidin mentioned, you can achieve it via the AWS CLI by forcing a new deployment like so:

aws ecs update-service \
--service <service name> \
--cluster <cluster name> \
--force-new-deployment \
[--profile guestapi-dev]

Note that this does not work on services with a CodeDeploy deployment controller.

Original answer:

I faced the same challenge, and what I did was follow this guide (using the old or new console depending on your service). I don't know if this can be done via the CLI, but it does actually "restart the service" in that it re-spawns new task(s) for your service and kills the old one(s).

In summary:

In the old console:

  1. Find the service in the AWS console (ECS -> Cluster -> Service).
  2. Click Update in the top right corner.
  3. Check the ‘Force new deployment’ box.
  4. Skip the other configurations and click Update Service.

In the new console:

  1. Find the service in the AWS console (ECS -> Cluster -> Service).
  2. Click Edit in the top right corner.
  3. Expand Deployments options
  4. Check the ‘Force new deployment’ box.
  5. Click Update.

The service will re-deploy. You should be able to see the existing task(s) running, the new task(s) provision and lastly the old task(s) disappear.

like image 29
stemadsen Avatar answered Oct 18 '22 18:10

stemadsen


this worked for me:

aws ecs list-tasks --cluster my-cluster-name | jq -r ".taskArns[]" | awk '{print "aws ecs stop-task --cluster my-cluster-name --task \""$0"\""}' | sh
like image 2
user326608 Avatar answered Oct 18 '22 18:10

user326608


Go to ECS dashboard. Just stop the running task from your ECS service from aws console. it'll spawn a new task and terminate the old one.

like image 1
Saravanan T Avatar answered Oct 18 '22 18:10

Saravanan T