Search code examples
terraformcontainersamazon-ecsterraform-provider-aws

Terraform access to ECS container UUID


If I use Terraform to create an ECS task definition...

    resource "aws_ecs_task_definition" "ecs_container" {
      family = "${var.component}-ecs-${var.variant}"
      task_role_arn = "${aws_iam_role.ecs_task_role.arn}"
      execution_role_arn = "${aws_iam_role.ecs_taskexec_role.arn}"
      container_definitions = <<EOT
    [
      {
        "name": "${var.component}",
        "image": "${var.ecrurl}/${var.component}-${terraform.workspace}:latest-${var.variant}",
        "cpu": 2,
        "memoryReservation": 256,
        "privileged": true,
        "essential": true
      }
    ]
    EOT
    }

...I can retrieve the taskId via the 'arn' attribute of the resulting aws_ecs_task_definition object. However, the format of the arn is using the ECS family:version style of the taskdef identity.

Is there a way to instead see the "container instance UUID" format? Eg, the style shown in the "Task" column of the Tasks tab, or as returned by aws ecs list-tasks?

{
    "taskArns": [
        "arn:aws:ecs:us-east-1:<aws_account_id>:task/0cc43cdb-3bee-4407-9c26-c0e6ea5bee84",
        "arn:aws:ecs:us-east-1:<aws_account_id>:task/6b809ef6-c67e-4467-921f-ee261c15a0a1"
    ]
}

Solution

  • However, the format of the arn is using the ECS family:version style of the taskdef identity.

    ...

    Is there a way to instead see the "container instance UUID" format?

    This is not an issue of the formatting of the ID. These are two completely separate IDs for two completely separate type of AWS resources.

    You are confusing Task Definitions and running Tasks. An ECS Task Definition, and a running ECS Task are two completely different things. Terraform is correctly giving you the IDs for the Task Definition as an attribute on the aws_ecs_task_definition resource.

    If you are familiar with EC2, then you can think of the ECS Task Definition as an EC2 launch configuration, and the running ECS Task ID as an EC2 instance ID.

    Terraform does not directly run ECS tasks. Note that there is no ability (at this time) to directly invoke the AWS ECS RunTask API via the Terraform AWS provider. Terraform can create ECS services, or ECS task schedules, or other EventBridge triggers to run ECS tasks, which eventually run ECS tasks, but Terraform does not run those tasks directly as part of a terraform apply. The running ECS tasks themselves are not managed by Terraform.

    Those task UUIDs you are trying to get access to are not static values. They can change constantly as ECS deployments, triggers, and auto-scaling occur. As such, this is not tracked in the Terraform state, and there is no method in Terraform to look these values up.

    Note that it can also take a while after running terraform apply to update an ECS service before these values are even available. After running terraform apply you would generally want to run a command to wait for the service deployment to complete, before querying the current ECS task IDs, like so:

    aws ecs wait services-stable --cluster mycluster --services myservice
    aws ecs list-tasks --cluster mycluster --service myservice