How do I check if an active task definition is in use by Amazon ECS resources?

3 minute read
0

I want to know if an active task definition is in use by an Amazon Elastic Container Service (Amazon ECS) service, task set, deployment, or standalone task before I delete it.

Resolution

Use the AWS Command Line Interface (AWS CLI) to determine whether Amazon ECS resources use a particular active task definition. Then, use the output to locate and deactivate unnecessary active task definitions. It's a best practice to verify the status of task definitions before you remove them.

Note: If you receive errors when you run AWS CLI commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

Open the AWS CLI, and then run the export command to set the region_name and task_definition_family environment placeholders. Replace the example_ values with your AWS Region and your task definition family's name:

echo "Listing all task definitions that are in use by ECS resources in your Region $region_name"

for cluster_name in $(aws ecs list-clusters --region $region_name --output json | jq -r '.clusterArns[] | split("/") | .[1]')
do

  for service_name in $(aws ecs list-services --cluster $cluster_name --region $region_name --output json | jq -r '.serviceArns[] | split("/") | .[2]')
  do
    taskdef_arn=$(aws ecs describe-services --cluster $cluster_name --services $service_name --region $region_name --output json | jq -r '.services[].taskDefinition')
    echo "Service Name: $service_name | Task Definition: $taskdef_arn | Cluster: $cluster_name"
  done

  for task_id in $(aws ecs list-tasks --cluster $cluster_name --region $region_name --family $task_definition_family --output json | jq -r '.taskArns[] | split("/") | .[2]')
     do

     result=$(aws ecs describe-tasks --tasks $task_id --cluster $cluster_name --region $region_name --output json)

     task_definition=$(echo $result | jq -r '.tasks[].taskDefinitionArn')

     task_group=$(echo $result | jq -r '.tasks[].group')

     echo "Task ID: $task_id | Group: $task_group | Task Definition: $task_definition | Cluster: $cluster_name"

    done

done

The following example output shows the list of task definitions that are in use:

Listing all task definitions that are in use by ECS resources in your Region example_region
  
Service Name: test123 | Task Definition: arn:aws:ecs:ap-southeast-1:1234567890:task-definition/nginx-fargate:16 | Cluster: enhanced-architecture-fargate
Service Name: test-ec2 | Task Definition: arn:aws:ecs:ap-southeast-1:1234567890:task-definition/httpd-ec2:4 | Cluster: enhanced-architecture-fargate
Task ID: 6af9a36954cb4ef0b238926135bc606b | Group: service:test123 | Task Definition: arn:aws:ecs:ap-southeast-1:1234567890:task-definition/nginx-fargate:16 | Cluster: enhanced-architecture-fargate
Task ID: e49c6b174d2b47789ac50597256afb88 | Group: family:nginx-fargate | Task Definition: arn:aws:ecs:ap-southeast-1:1234567890:task-definition/nginx-fargate:16 | Cluster: enhanced-architecture-fargate
Task ID: f1ac98cc3b37494ea681832f52d49a05 | Group: family:nginx-fargate | Task Definition: arn:aws:ecs:ap-southeast-1:1234567890:task-definition/nginx-fargate:16 | Cluster: enhanced-architecture-fargate
Service Name: test123 | Task Definition: arn:aws:ecs:ap-southeast-1:1234567890:task-definition/nginx-fargate:16 | Cluster: test1
Task ID: 95112b2b561b4bb98aff3877548a60ff | Group: service:test123 | Task Definition: arn:aws:ecs:ap-southeast-1:1234567890:task-definition/nginx-fargate:16 | Cluster: test1

Optional: To list all the task definitions that are in use for a particular Region, remove the --family option from the list-tasks API call. Use pagination options to list a large number of task definitions in an AWS Account for a Region.

Related information

Amazon ECS resources that can block a deletion

Amazon ECS task definition parameters

AWS OFFICIAL
AWS OFFICIALUpdated 20 days ago