Dilip
Dilip

Reputation: 385

How to connect two tasks in fargate

I have two tasks in faragte from two different task definitions. Now containers in the same tasks can access each other via localhost.

However, how do i make containers in two different tasks talk to one another.

I'm mainly trying to build a pipeline where each task defintion ( as a task) runs one by one. And one task my be dependant on the other.

Upvotes: 3

Views: 2476

Answers (2)

Amlan
Amlan

Reputation: 185

In Fargate, when multiple containers are launch as part of a single task, they can also communicate with each other over the local loopback interface. Fargate uses a special container networking mode called awsvpc, which gives all the containers in a task a shared elastic network interface to use for communication.

If you specify a port mapping for each container in the task, then the containers can communicate with each other on that port. For example, the following task definition could be used to deploy the web tier and the API tier:

JSON

  "family": "myapp"
  "containerDefinitions": [
    {
      "name": "web",
      "image": "my web image url",
      "portMappings": [
        {
          "containerPort": 80
        }
      ],
      "memory": 500,
      "cpu": 10,
      "esssential": true
    },
    {
      "name": "api",
      "image": "my api image url",
      "portMappings": [
        {
          "containerPort": 8080
        }
      ],
      "cpu": 10,
      "memory": 500,
      "essential": true
    }
  ]
}

ECS, with Fargate, is able to take this definition and launch two containers, each of which is bound to a specific static port on the elastic network interface for the task.

Because each Fargate task has its own isolated networking stack, there is no need for dynamic ports to avoid port conflicts between different tasks as in other networking modes. The static ports make it easy for containers to communicate with each other.

For example, the web container makes a request to the API container using its well-known static port:

 curl 127.0.0.1:8080/my-endpoint
 Bash

This sends a local network request, which goes directly from one container to the other over the local loopback interface without traversing the network. This deployment strategy allows for fast and efficient communication between two tightly coupled containers.

Upvotes: 0

Chris Williams
Chris Williams

Reputation: 35188

Rather than using them to communicate to one other look at using an orchestration layer which can control the flow.

Step functions is an AWS solution for this and it supports ECS and Fargate so will take responsibility of launch tasks as they are needed.

Rather than the tasks communicating directly with each other each step can provide output, and take in input.

By using this approach containers are isolated from each other but still maintain the ability to be performed in sequence. It will also provide visibility as to what is happening, with support for waiting, loops and success/failure cases.

Upvotes: 1

Related Questions