aws-actions / amazon-ecs-deploy-task-definition

Registers an Amazon ECS task definition and deploys it to an ECS service.
MIT License
649 stars 241 forks source link

Is it possible to tag tasks? #249

Open karanpratapsingh opened 3 years ago

karanpratapsingh commented 3 years ago

Looking at the code, I see you use codedeploy.createDeployment function which doesn't doesn't allow for tagging task that'll be created like we can do with ecs.runTask...is there a possible approach?

Lou1415926 commented 3 years ago

Hello @karanpratapsingh ! Sorry for the late check-in 🙇🏼‍♀️ !

This action helps you update an existing ECS service with a task definition. As the starting and stopping of a task is controlled by the ECS service that it runs under, the action itself does not have the ability or permission to tag the tasks.

Here are a few suggestions that I have to achieve your goal:

Propagate Tags From Service Or Task Definition

If you specify PropagateTags at the time of your service creation, your tasks can be automatically tagged when it is spun up. This option is available in the AWS console experience, aws-cli, sdk, etc. However, it is only configurable at service creation time.

Tagging The Service

You can also just tag the service, and not the individual tasks. In order to reach the tasks spun up by that service, you can use the ListTasks API. You can tag the service through console, aws-cli, sdk, etc.

Tag Resources Manually

Finally, you can still opt to tag your individual tasks using the tag-resources API. Again, in aws-cli, from AWS console, whichever tools you like to use. This requires you to know the resource ARN, so it might be a bit of churn if you are looking to tag your tasks individually every time they are spun up :(

You can find more information about tagging ECS resources here as well.

Hope this helps!!

karanpratapsingh commented 3 years ago

Thanks for the detailed answer @Lou1415926 , I ended up doing it with cli. Here's the script

#!/bin/bash

CLUSTER="$1"
SERVICE="$2"

PLATFORM_TAG="key=platform,value=Fargate"

# Tag new ECS tasks
for ARN in $(aws ecs list-tasks --cluster $CLUSTER --output text --query 'taskArns[*]' --service $SERVICE --no-cli-pager);
do
  echo "Tagging ARN: $ARN with $PLATFORM_TAG"
  aws ecs tag-resource --resource-arn $ARN --tags $PLATFORM_TAG --no-cli-pager;
done
usage: ./tag.sh staging-cluster svc-abc
dgokcin commented 1 year ago

I see that this is an old issue but I would like to ask for your recommendation @Lou1415926 @karanpratapsingh

We have deployed our service with terraform and and the majority of our services were created without the ecs_service_tags. Than after a while we wanted to use those tags for some reporting purposes(cost dashboards) So after service creation we adderd the ecs_service_tags option is added to our terraform modules. We noticed that after a terraform apply, our resources were tagged properly but once a new docker imags is built in our CI and deployed using this action, the tags are gone.

We suspect that, because of the --query taskDefinition parameter in the action, although the output of the aws ecs describe-task-definition --region eu-west-1 --task-definition my-task-def-name --include TAGS > task_def command has tags, once the query option is added, the tags are not written to the task_def file and naturally, tags are gone in the next task definition.

Any idea what what approach should I do if I want the tags to persist and I do not want to avoid re-creating the services or using a script each time there is a deployment.

Thanks!