Search code examples
amazon-web-servicesamazon-ecsamazon-vpcaws-fargate

Cannot talk to another container inside same task on ECS Fargate using awsvpc networking type


I am running a standard SPA frontend app and a node server in the same ECS task.

Everything that I have read about awsvpc can use localhost when 2 containers inside the same task are interfacing with each other.

However, with this set up I can't seem to return data from my node server to my UI. All my requests immediately fail. I have asserted that it is actually trying to hit localhost on my actual computer.

Browser failure: https://pasteboard.co/JFJLnLO.png

For testing purposes I exposed port 8080 to see if I could interact with the node server directly and that works as expected. I just cant get the UI to talk to it.

Any help would be much appreciated

EDIT:

My task definition looks like this:

  "containerDefinitions": [
    {
      "essential": true,
      "image": "[my-account-id].dkr.ecr.eu-west-1.amazonaws.com/[my-account]/app-ui:latest",
      "name": "app-ui",
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/app-ui",
          "awslogs-region": "eu-west-1",
          "awslogs-stream-prefix": "ecs",
          "awslogs-create-group": "true"
        }
      },
      "portMappings": [
        {
          "containerPort": 3000,
          "hostPort": 3000,
          "protocol": "tcp"
        }
      ]
    },
    {
      "essential": true,
      "image": "[my-account-id].dkr.ecr.eu-west-1.amazonaws.com/[my-account]/app-api:latest",
      "name": "app-api",
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/app-api",
          "awslogs-region": "eu-west-1",
          "awslogs-stream-prefix": "ecs",
          "awslogs-create-group": "true"
        }
      },
      "portMappings": [
        {
          "containerPort": 8080,
          "hostPort": 8080,
          "protocol": "tcp"
        }
      ]
    }
  ],
  "cpu": "256",
  "executionRoleArn": "arn:aws:iam::[my-account-id]:role/AWSServiceRoleECS",
  "family": "app",
  "memory": "512",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"]
}


  [1]: https://i.sstatic.net/S7zl3.png

Solution

  • Based on the comments.

    The issue is caused by calling localhost api endpoint in on the client side in the browser. This will resolve to localhost on the client machine, not within the ECS tasks. The localhost will work when the API is called from the inside of the ECS task, not from outside of the task.

    To call the API endpoint from the client side, regular public IP or public DNS is required, not localhost.