Terraform: AWS ECS TaskDefinition - odd family and container name requirement

Created on 30 Jul 2015  ยท  8Comments  ยท  Source: hashicorp/terraform

I'm running into "The container X does not exist in the task definition."

Terraform is expecting to find a container with the same name as the task definition family/name of the task definition. This is not a requirement in ECS, so I'm curious if this is a bug or if there is a workaround. Having the task definition name have to be the same as the container name makes deployment transposing across environments a pain.

Thoughts/ Ideas?

bug provideaws waiting-response

Most helpful comment

I found this issue while searching for:

ClientException: Container.name should not be null or empty.

My problem was that I copied and pasted the entire JSON structure from another task definition without noticing that the container_definitions option should only be set to the containerDefinitions section of the task definition (not the entire thing). It looks like @daytonpa did the same thing as me. Therefore, his example should actually look like this:

container_definitions = <<EOF
[
  {
    "portMappings": [
      {
        "hostPort": 80,
        "protocol": "tcp",
        "containerPort": 3000
      },
      {
        "hostPort": 3001,
        "protocol": "tcp",
        "containerPort": 3001
      }
    ],
    "cpu": 4096,
    "memory": 7200,
    "memoryReservation": 7000,
    "image": "my_repo_url/app",
    "essential": true,
    "name": "prod-app-container"
  }
]

Hope this helps.

All 8 comments

I'm unable to reproduce this, using the following example created based on what you described:

provider "aws" {
    region = "us-west-2"
}

resource "aws_ecs_task_definition" "boom" {
  family = "meh"
  container_definitions = <<DEF
[
  {
    "name": "blah",
    "image": "jenkins",
    "cpu": 10,
    "memory": 500,
    "essential": true,
    "portMappings": [
      {
        "containerPort": 80,
        "hostPort": 80
      }
    ]
  }
]
DEF

  volume {
    name = "some-home"
    host_path = "/ecs/some-home"
  }
}

Do you have any example that you can share, so we can reproduce it?

My apologies - I didn't clarify the issue well enough. It wasn't the task definition throwing the error - It was when assigning that task definition to the service. After running terraform - I would find the successfully created task definition - but the service creation would fail with the previously stated complaint.

@Spicykoala I suspect you're trying to registrater ECS service to an ELB. I did test an example which has a unique container name (different from service name). Look for ghostcont in the attached gist.

Everything worked aok.

Can you please attach any code that would help us to reproduce this?

You're 100% right about the ECS service trying to register to the ELB using an incorrect container name.

I was having trouble getting the service updated with new ELB info via terraform (I think to change the ELB info requires a destroy on the service, but it wasn't giving that option). Either way, that's a separate issue, if it's even issue at all.

Thanks for all the awesome work.

I am getting the following error as well:

Error: Error applying plan:

1 error(s) occurred:

* aws_ecs_task_definition.prod: 1 error(s) occurred:

* aws_ecs_task_definition.prod: ClientException: Container.name should not be null or empty.
    status code: 400, request id: 0ac0c039-5ad8-11e8-af13-e3c4d57a1ef5

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

Here is my ECS tf file:

data "aws_ecr_repository" "app_prod" {
  name = "docnetwork/app"
}

resource "aws_ecs_task_definition" "prod" {
  family = "prod-application-backend"
  container_definitions = <<EOF
[
  {
    "containerDefinitions": [
      {
        "portMappings": [
          {
            "hostPort": 80,
            "protocol": "tcp",
            "containerPort": 3000
          },
          {
            "hostPort": 3001,
            "protocol": "tcp",
            "containerPort": 3001
          }
        ],
        "cpu": 4096,
        "memory": 7200,
        "memoryReservation": 7000,
        "image": "my_repo_url/app",
        "essential": true,
        "name": "prod-app-container"
      }
    ],
    "compatibilities": [
      "EC2"
    ],
    "family": "prod-application-backend",
    "requiresAttributes": [
      {
        "name": "com.amazonaws.ecs.capability.ecr-auth"
      },
      {
        "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
      }
    ]
  }
]
EOF
}
resource "aws_ecs_cluster" "prod" {
  name = "production"
}
resource "aws_ecs_service" "prod" {
  name = "prod-backend-service"
  cluster = "${aws_ecs_cluster.prod.id}"
  task_definition = "${aws_ecs_task_definition.prod.arn}"
  desired_count = "${var.ecs_service_desired_count}"

  depends_on = ["aws_ecs_cluster.prod", "aws_ecs_task_definition.prod", "aws_alb_target_group.prod", "aws_autoscaling_group.prod"]
}

As you can see, the name is inside the TD, but the resource is still unhappy. I have an Auto Scaling Group and Launch Config that takes care of the needed hardware to run the container. Any thoughts? Using Terraform v0.11.5

@daytonpa any luck with this issue? I am getting the exact same error :(

I found this issue while searching for:

ClientException: Container.name should not be null or empty.

My problem was that I copied and pasted the entire JSON structure from another task definition without noticing that the container_definitions option should only be set to the containerDefinitions section of the task definition (not the entire thing). It looks like @daytonpa did the same thing as me. Therefore, his example should actually look like this:

container_definitions = <<EOF
[
  {
    "portMappings": [
      {
        "hostPort": 80,
        "protocol": "tcp",
        "containerPort": 3000
      },
      {
        "hostPort": 3001,
        "protocol": "tcp",
        "containerPort": 3001
      }
    ],
    "cpu": 4096,
    "memory": 7200,
    "memoryReservation": 7000,
    "image": "my_repo_url/app",
    "essential": true,
    "name": "prod-app-container"
  }
]

Hope this helps.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings