Question: How do I troubleshoot Amazon ECS tasks that take a long time to stop when the container instance is set to DRAINING?

My Amazon Elastic Container Service (Amazon ECS) task is taking a long time to move to the STOPPED state. Or, my Amazon ECS task is stuck in the RUNNING state when the container instance is set to DRAINING. How can I resolve this issue?Short descriptionWhen you set an ECS instance to DRAINING, Amazon ECS does the following:Prevents new tasks from being scheduled for placement on the container instanceStops tasks on the container instance that are in the RUNNING stateYour tasks can be stuck in the RUNNING state or take a longer time to move to the STOPPED state due to issues with configuration parameters or tasks. To troubleshoot these issues, consider the following options:Confirm that your DeploymentConfiguration parameters are set correctlyConfirm that the deregistration delay value is set correctlyConfirm that the ECS_CONTAINER_STOP_TIMEOUT value is set correctlyLook for other task-related issues

ResolutionConfirm that your DeploymentConfiguration parameters are set correctlyOpen the Amazon ECS console.In the navigation pane, choose Clusters, and then choose the cluster where your container instance is draining.Choose the ECS Instances tab, and then choose DRAINING in the Status section.Choose your container instance, and then find out the service for the tasks that are draining or taking a long time to drain.Choose the Services tab, select the service, and then choose Deployments.Check the values for minimumHealthyPercent and maximumPercent.Note: Service tasks on the container instance that are in the RUNNING state are stopped and replaced according to the service's deployment configuration parameters: minimumHealthyPercent and maximumPercent.Confirm that the deregistration delay value is set correctlyImportant: The following steps apply only to services using the Application Load Balancer or Network Load Balancer. If your service is using the Classic Load Balancer, check the connection draining values.Open the Amazon ECS console.In the navigation pane, choose Clusters, and then choose the cluster where your container instance is draining.Choose the Services tab, and then select the service with the stack stuck in RUNNING.Choose Target Group Name.On the Details tab, scroll down, and then select the Deregistration delay check box.Confirm that the ECS_CONTAINER_STOP_TIMEOUT value is set correctlyConnect to your container instance using SSH.Run the docker inspect ecs-agent --format '{{json .Config.Env}}' command.Check if there is a value for ECS_CONTAINER_STOP_TIMEOUT.Note: ECS_CONTAINER_STOP_TIMEOUT is an ECS container agent parameter that defines the amount of time that Amazon ECS waits before ending a container. The time duration starts counting when a task is stopped. If you don't see the ECS_CONTAINER_STOP_TIMEOUT parameter in the output after running the command in step 2, then Amazon ECS is using the default value of 30s.Look for other task-related issuesConnect to your container instance using SSH.Verify that the Docker daemon and Amazon ECS container agent are running for either your Amazon Linux 1 AMIs or Amazon Linux 2 AMIs.Check the application logs based on the log driver set by logConfiguration.Note: For example, if your tasks are using the awslogs log driver, check your Amazon CloudWatch Logs for issues.