Search code examples
terraformamazon-ecs

AWS - Tags on ECS tasks not being inherited


I'm taking my first steps on the DevOps life, and i've encountered a rather annoying issue while trying to use tags on ECS with Terraform.
I've already tried using tags on the task definition template, but they're doing nothing.
I've been able to tag correctly both the ECS cluster AND the services... but i can't seem to be able to tag the actual tasks

Thanks!


Solution

  • ECS Task definitions can be tagged:

    resource "aws_ecs_task_definition" "task" {
      # ...
      tags = {
        ImATag = "yes indeed"
        AnotherTag = "another one"
      }
      # ...
    }
    

    ECS services can be tagged as well and configured to propagate their tags to tasks:

    resource "aws_ecs_service" "service_with_auto_scaling" {
      # ...
      tags           = {
        ImATag = "yes indeed"
        AnotherTag = "another one"
      }
      propagate_tags = "SERVICE"
      # ...
    }
    

    If you want the tasks from the ECS Task Definition to propagate to the ECS Tasks themselves, you can do this instead (thank you @williamfalconeruk for the additional info here!):

    resource "aws_ecs_service" "service_with_auto_scaling" {
      # ...
      tags           = {
        ImATag = "yes indeed"
        AnotherTag = "another one"
      }
      propagate_tags = "TASK_DEFINITION"
      # ...
    }
    

    You need to log in as root and go to the ECS service settings and enable the new resource IDs for propagate_tags to work.