amazon-web-servicesaws-lambdaamazon-ecs

How to get the service to which the ECS task belongs


I am using AW Lambda, which consumes ECS task events. Based on task state change event I need to get the corresponding service (ECS service name).

Right now I use Task Group to retrieve it, but I've found that it does not work for all of the tasks (some tasks have groups with 'family' prefix). Also going through all services and checking their tasks is not an option (there are a lot of services and Lambda has a few seconds timeout).

I saw that apparently task definition ARN contains service name, but I am not sure that it is the rule for AWS ARN generation (could not find any docs), but rather some custom definition ARN generation (which won't apply to all of the tasks). What would be the best approach in this situation?


Solution

  • Turns out the task which have a corresponding service have a group with this pattern : "service:{service_name}", and the tasks with the group starting with "family" are standalone tasks

    Blockquote By default, standalone tasks use the task definition family name (for example, family:my-task-definition) as the task group name if a custom task group name isn't specified. Tasks launched as part of a service use the service name as the task group name and can't be changed. Blockquote