How do I link 2 containers running in a AWS ECS task

With Fargate, If you want to access your backend using localhost:42048, then you can try configuring your Frontend and Backend in the same Task definition. While deploying the task, all the containers defined in the same task definition would run in the same underlying host and we can access it using localhost. Remember that Fargate storage is ephemeral and your backend shouldn't maintain application state in the container.

...
"containerDefinitions": [
    {
      "name": "frontend",
      "image": "my-repo/angularapp",
      "cpu": 256,
      "memory": 1024,
      "essential": true,
      "portMappings": [ {
          "containerPort": 8080,
          "hostPort": 8080
       }
     ]
    },
    {
      "name": "backend",
      "image": "my-repo/springboot",
      "cpu": 256,
      "memory": 1024,
      "essential": true,
      "portMappings": [ {
          "containerPort": 42048,
          "hostPort": 42048
       }
     ]
    }
  ]
...  

But I'm afraid this approach isn't suitable for production grade.


linking is not allowed in AWSVPC.

You can do linking only in network mode when its set to bridge.

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. For more information about linking Docker containers, go to https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/. This parameter maps to Links in the Create a container section of the Docker Remote API and the --link option to docker run.

Note

This parameter is not supported for Windows containers or tasks using the awsvpc network mode.

Important

Containers that are collocated on a single container instance may be able to communicate with each other without requiring links or host port mappings. Network isolation is achieved on the container instance using security groups and VPC settings.

task_definition_parameters

In network mode, you have to define two containers in the same task definition and then mentioned the name of the container in the link.

enter image description here And then Mentioned the name of backend container in frontend container.

enter image description here