Home > front end >  How can I update the option "propagateTags" on an already created ECS service?
How can I update the option "propagateTags" on an already created ECS service?

Time:01-20

I need to tag all the existing and coming task definitions for a given ECS service. The issue is that I realized that the "PropagateTags" property of the service is set to "NONE" and the tasks definitions do not have the required tags. I attempted to update that property in the console first, but it was not possible. Then, I used the next command:

aws ecs update-service --cluster my-cluster --service my-service --propagateTags  SERVICE --profile "my-profile"

But that didn't work either, because the argument "propagateTags" was not recognized.

My last attempt was using boto3 (API) using the next script:

import boto3

session = boto3.Session(profile_name = 'my-profile', region_name = 'us-east-1')
    
ecs_cluster= 'my-cluster'
ecs_service= 'my-service'

ecs_client=boto3.client('ecs')

response = ecs_client.update_service(
    cluster=ecs_cluster,
    service=ecs_service,
    propagateTags='SERVICE'
)

But, guess what? That didn't work because the API call didn't recognize the tags argument.

Traceback (most recent call last):
  File "./ecs_propagatetags.py", line 16, in <module>
    propagateTags='SERVICE'
  File "C:\Python37\lib\site-packages\botocore\client.py", line 388, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Python37\lib\site-packages\botocore\client.py", line 681, in _make_api_call
    api_params, operation_model, context=request_context)
  File "C:\Python37\lib\site-packages\botocore\client.py", line 729, in _convert_to_request_dict
    api_params, operation_model)
  File "C:\Python37\lib\site-packages\botocore\validate.py", line 360, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Unknown parameter in input: "propagateTags", must be one of: cluster, service, desiredCount, taskDefinition, capacityProviderStrategy, deploymentConfiguration, networkConfiguration, placementConstraints, placementStrategy, platformVersion, forceNewDeployment, healthCheckGracePeriodSeconds, enableExecuteCommand

Any ideas on how I can resolve the issue? Recreating the service is the very last option.

CodePudding user response:

The documentation shows propagateTags as a valid parameter for the ECS update_service() method so if your version of boto3 is saying it isn't valid you probably need to upgrade your version of boto3.

Same with the AWS CLI so you probably aren't running the latest version of that either.

Once you get the propagateTags configuration set on your service, it will still only apply to any new tasks that it creates, the existing running tasks will not get tags added to them. So you can either trigger a new service deployment to get the service to replace those tasks with new tasks that have tags assigned to them, or you can use the ECS tag_resource() method to add tags to the existing tasks.

CodePudding user response:

Unfortunately, that seems to be the only documented option: propagateTags (string) -- Specifies whether to propagate the tags from the task definition to the task. If no value is specified, the tags aren't propagated. Tags can only be propagated to the task during task creation. To add tags to a task after task creation, use the TagResource API action. Ref: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ecs.html

As it mentions there, you can use the tag_resource client method though: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ecs.html#ECS.Client.tag_resource

  • Related