Python - Pulling ECS Service logs for a task/container that is terminating

0

I have a python script which is grabbing logs for a ECS cluster that has multiple services. It works for tasks that are running without any issues. I have tasks in few services that are terminating due to errors in the container scripts. I can see the logs/erros when I go to the ECS Cluster -> Click on Service and go to the Logs tab. How do I modify the script to grab those logs thrown by tasks that have terminated

My python script has the following logic.

response = ecs_client.list_services(cluster=cluster_name)
for service in response['serviceArns']:
  serviceResponse = ecs_client.describe_services(cluster=cluster_name, services=[service])
  serviceName = serviceResponse['services'][0]['serviceName']
  taskResponse = ecs_client.list_tasks(
            cluster=cluster_name,
            serviceName=serviceName
  )

 if 'taskArns' not in taskResponse or not taskResponse['taskArns']:
            print(f"No tasks found for {serviceName}")

Since the task is terminating, the script always ends up saying "No tasks found.."

已提問 6 個月前檢視次數 399 次
1 個回答
1
已接受的答案

By default it looks for running tasks. You need to look for stopped tasks.

taskResponse = ecs_client.list_tasks(
            cluster=cluster_name,
            serviceName=serviceName,
            desiredStatus='STOPPED'
 )

desiredStatus (string) – The task desired status to use when filtering the ListTasks results. Specifying a desiredStatus of STOPPED limits the results to tasks that Amazon ECS has set the desired status to STOPPED. This can be useful for debugging tasks that aren’t starting properly or have died or finished. The default status filter is RUNNING, which shows tasks that Amazon ECS has set the desired status to RUNNING.

profile picture
專家
已回答 6 個月前
profile pictureAWS
專家
已審閱 5 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南