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

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달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠