How to communicate between containers in the same task in AWS ECS?

Viewed 1529

I have a service-A with a task definition task-A that includes multiple container definitions, for example nginx and grafana. How can these containers communicate with each other? The network used is the default bridge network.

I have tried curl grafana:3000, but the container is not able to resolve the name. If I would try the same on my local machine it would work. What am I missing?

Here is my task definition:

resource "aws_ecs_task_definition" "this" {
  family = "x"
  execution_role_arn = "x"
  task_role_arn = "x"
  container_definitions = jsonencode(local.task_definition)
}

Container definition excerpt:

locals {
  task_definition = [
    {
      name: "nginx",
      image: "nginx:latest",
      portMappings: [{
        containerPort: 80,
        hostPort: 0,
        protocol: "tcp"
      }],
      dependsOn: [{
        "containerName": "grafana",
        "condition": "START"
      }]
    },
    {
      name: "grafana",
      image: "grafana/grafana:latest",
      portMappings: [{
        containerPort : 3000,
        hostPort: 0,
        protocol: "tcp"
      }]
    }
  ]
}
1 Answers

dependsOn only work to start the container in order, you need linking to make service to service level communication between container.

      dependsOn: [{
        "containerName": "grafana",
        "condition": "START"
      }]
      "links": [
        "grafana"
      ]

From Doc

links
Type: string array

Required: no

The link parameter allows containers to communicate with each other without the need for port mappings. Only supported if the network mode of a task definition is set to bridge. The name:internalName construct is analogous to name:alias in Docker links. Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed

communicate-between-containers-in-the-same-task-in-aws-ecs

Related