3
votes

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
TBH: I'm not very familiar with AWS ECS. But have you heard of "Amazon ECS Service Discovery"? By reading the related blog-article it seems like it's exactly what you need: aws.amazon.com/de/blogs/aws/amazon-ecs-service-discovery - Hermsi1337
Yeah I have heard of it and it is in my backlog to implement a proper service discovery solution either with AWS services or something like Consul. What I am trying to achieve is the very basic functionality I already get with running Docker containers in a single Docker network. Seems like that is not supported in AWS ECS - trallnag

1 Answers

5
votes

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