<@ULVA73B9P> How would I use the `Labels` settings...
# ask-marvin
d
@Marvin How would I use the
Labels
settings field in my ECS hosted work-pool to add tags to spawned ECS tasks for my flow deployments
m
thought for 98.0 seconds
Short answer: you can’t. The Work Pool “Labels” field is Prefect metadata used for organizing/routing work; it does not propagate to AWS as ECS task tags. How to add AWS tags to the ECS tasks Prefect spawns - Set default tags on the pool (Default base job variables), or - Override per-deployment via
job_variables
Most users just add a
tags
section to the ECS worker’s job variables. Example defaults on your ECS work pool:
Copy code
{
  "cluster": "my-ecs-cluster",
  "task_definition_arn": "arn:aws:ecs:us-east-1:123456789012:task-definition/my-task:42",
  "network_configuration": {
    "awsvpcConfiguration": {
      "subnets": [
        "subnet-abc",
        "subnet-def"
      ],
      "securityGroups": [
        "sg-123"
      ],
      "assignPublicIp": "ENABLED"
    }
  },
  "tags": {
    "Project": "analytics",
    "Environment": "prod",
    "Owner": "data-eng"
  }
}
Per-deployment override (Prefect 3.x):
Copy code
from prefect import flow

@flow
def my_flow():
    ...

if __name__ == "__main__":
    (
        my_flow
        .from_source(
            "<https://github.com/acme/prefect-flows>",
            entrypoint="flows/my_flow.py:my_flow",
        )
        .deploy(
            name="prod",
            work_pool_name="ecs-pool",
            job_variables={
                "tags": {
                    "Project": "analytics",
                    "Environment": "prod",
                    "Owner": "data-eng"
                }
            },
        )
    )
Notes and gotchas - These “tags” are AWS ECS task tags; you’ll need IAM permissions that allow tagging on
ecs:RunTask
(e.g.,
ecs:TagResource
via RunTask). - If you already put tags on your Task Definition and want them on the Task, you can rely on ECS propagation if your setup supports it; otherwise pass tags directly as above. - If you don’t see tags on the running task, double-check your pool’s “Default base job variables” and the deployment’s
job_variables
(deployment overrides pool defaults). Docs - ECS worker guide: Prefect AWS ECS guide If you share your current pool “Default base job variables,” I can point out exactly where to add the tags. Yes, I know, thrilling.
d
@Marvin @Nate This is wrong btw, the
Labels
field in the work-pool settings page can be used to add tags, Prefect just adds them on top of the default ones they have when registering the task-run
m
thought for 114.4 seconds
Appreciate the correction. If your ECS worker is turning Work Pool “Labels” into AWS task tags, that’s great—and it means Prefect is merging them with its default tags at run time. I haven’t seen this documented in the ECS guide, so a couple clarifications would help me give you an exact mapping: which Prefect version are you on, and what Key/Value do you see for label-based tags (Key=label, Value="true"/""/label)? What I can confirm/practice that works today: - Prefect adds its own default AWS tags (e.g., flow/deployment identifiers) when calling
RunTask
. - Any
tags
you set in the ECS job variables are included and are the most explicit, predictable way to control AWS tags. - If your worker is also propagating Work Pool labels, they’ll be merged “on top” of Prefect’s defaults; typically deployment
job_variables.tags
should win on key conflicts. Explicit, reliable way (pool defaults or per-deployment): - Pool “Default base job variables”:
Copy code
{
  "tags": {
    "Project": "analytics",
    "Environment": "prod"
  }
}
- Per-deployment:
Copy code
from prefect import flow

@flow
def my_flow():
    ...

if __name__ == "__main__":
    (
        my_flow
        .from_source(
            "<https://github.com/acme/prefect-flows>",
            entrypoint="flows/my_flow.py:my_flow",
        )
        .deploy(
            name="prod",
            work_pool_name="ecs-pool",
            job_variables={
                "tags": {
                    "Project": "analytics",
                    "Environment": "prod"
                }
            },
        )
    )
If you want to rely on Work Pool “Labels,” go ahead and add them there; they should be merged with Prefect’s defaults at task submission. If you can share a quick example of the resulting tag keys/values you see in AWS (or your Prefect version), I’ll document the exact mapping and precedence for you. Until then,
job_variables.tags
remains the most explicit way to guarantee AWS tags. Docs for reference: - ECS worker guide: Prefect AWS ECS guide