Check if task exist - AWS ECS

Viewed 489

How do you check from cli or using a shell script if the task exists?

The documentation is confusing. Is using the --task-definition option as family.

The command

TASK=$(aws ecs describe-task-definition --task-definition ${TASK_NAME}

it errors saying

An error occurred (ClientException) when calling the DescribeTaskDefinition operation: Unable to describe task definition.

Any ideas?

1 Answers

Does this help?

#!/bin/bash

ecs_arn=$1

aws_result=$(aws ecs describe-task-definition --task-definition "$ecs_arn" --query 'taskDefinition.[taskDefinitionArn,status]' --output json 2>/dev/null)

# is it present?
if [ $(echo "$aws_result" | grep -i "$ecs_arn" | wc -l) -ne 1 ]; then
    echo "task-definition $ecs_arn doesn't exist."
elif [ $(echo "$aws_result" | grep -i active | wc -l) -ne 1 ]; then
    echo "task-definition $ecs_arn exist but not active."
else
    echo "task-definition $ecs_arn exist and it's active."
fi

# end

Save it as bash script and execute it like:

./script.sh "arn:aws:ecs:REGION-HERE:ACCOUNT-NAME:............."

You can extract all definition ARNs with:

aws ecs list-task-definitions

I'm redirecting std errors to /dev/null because the api returns error like yours when the task doesn't exist.

You can modify the validations as you like in your case.

Documentation used: https://docs.aws.amazon.com/cli/latest/reference/ecs/index.html

Related