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.