Skip to main content Miguel Hernández

Propagating Tags to AWS ECS Tasks

TL;DR
By default, user-defined tags on an AWS ECS Service are not propagated to its Tasks, which can impact cost allocation. This can be fixed by setting the propagate_tags parameter.

If you use AWS tags on your resources for cost allocation, you might run into a surprise with Amazon ECS. I discovered that when you assign user-defined tags to an ECS Service, those tags are not automatically propagated to the Tasks that the service launches.

This is a significant issue because if your cost tracking reports rely on these tags, the costs associated with your ECS Tasks may not be correctly attributed, leading to unexpected expenses showing up as “untagged.”

The Root Cause

By default, ECS automatically applies its own managed tags (like the cluster name) to all newly launched tasks, but it does not do the same for the custom, user-defined tags you set on the service level.

The propagation of tags from a service or task definition to a task is controlled by a specific parameter that defaults to NONE. This means you have to explicitly enable this behavior.

The Solution

To ensure your user-defined tags are correctly passed down from the service to the tasks, you must enable tag propagation.

Using AWS CLI

When updating an existing service, you can force a new deployment and enable tag propagation using the --propagate-tags flag.

shell code snippet start

aws ecs update-service \
  --cluster <CLUSTER_NAME> \
  --service <SERVICE_NAME> \
  --force-new-deployment \
  --propagate-tags SERVICE

shell code snippet end

The valid values are SERVICE or TASK_DEFINITION, depending on where your source tags are defined.

Using Terraform

If you manage your infrastructure with Terraform, the fix is straightforward. You just need to set the propagate_tags argument within your aws_ecs_service resource block:

terraform code snippet start

resource "aws_ecs_service" "my_service" {
  # ... other configuration
  
  propagate_tags = "SERVICE" # or "TASK_DEFINITION"
}

terraform code snippet end

By enabling this setting, you ensure that your cost allocation tags are correctly applied, giving you an accurate picture of your ECS spending.