Reputation: 3654
we have automated creation of fargate services and task definitions from our CI system. Everything is named except task definitions, which just get a generated ID. We tag them with the information we need, but can't find an easy way of finding a task definition by tag, beyond asking for the list of all task definitions and looping through, describing them and getting tags.
can any tell me if there's another way - basically putting something in a task definition at creation time that I can just say "find me that task definition" later?
Upvotes: 2
Views: 2278
Reputation: 3654
actually - one of my co-workers figured it out - I told him to post here, but I guess he didn't. You just do this:
aws resourcegroupstaggingapi get-resources --region ${amazon_region} --resource-type-filters ecs:task-definition --tag-filters Key=*******,Values=***** Key=******,Values=****** --max-items 1 | jq --raw-output '.ResourceTagMappingList[].ResourceARN'
Upvotes: 5
Reputation: 162
I'm pretty late to the party here, but I don't think it's possible to reference a task definition by tag (at least not after an hour pouring over the AWS documentation). For our CD setup we had to run a script that grabs all active task definitions and finds the one with a release tag value we want. It looks like this:
"""
Fetches a task definition based on family name and the `release` tag value.
Usage:
python3 -m app.scripts.get_task_definition TASK_FAMILY RELEASE_TAG
"""
import sys
import boto3
task_family = sys.argv[1]
release = sys.argv[2]
ecs_client = boto3.client("ecs", region_name="us-west-2")
# Sort by newest first
list_response = ecs_client.list_task_definitions(familyPrefix=task_family, status="ACTIVE", sort="DESC")
for arn in list_response["taskDefinitionArns"]:
task_definition = ecs_client.describe_task_definition(taskDefinition=arn, include=["TAGS"])
task_release_tags = [tag for tag in task_definition["tags"] if "release" in tag["key"]]
task_release = task_release_tags[0].get("value", None) if task_release_tags else None
if task_release == release:
print(arn)
Upvotes: 0