15

I am new to ECS and I am trying to deploy a couple of containers in a ECS task using Fargate.

I have 1 container running that uses Angular2 and is running on nginx, the other container is the backend and is running on Springboot and uses the port 42048.

I am using the awsvpc network with Fargate and I have to do it that way.

The Angular app communicates with the backend using localhost:42048/some_url and it works fine in my local docker but in AWS the front-end doesn't find the backend. Currently I have my ports mapped with 80 for the front end and 42048 for the backend and the front-end when deployed locally was able to find the backend as localhost:42048

Any help would be appreciated. Thank you

Linkavich14
  • 243
  • 1
  • 5
  • 15
  • 1
    https://docs.aws.amazon.com/en_us/AmazonECS/latest/developerguide/example_task_definitions.html?shortFooter=true#example_task_definition-wordpress – Passatizhi Jun 11 '19 at 02:37
  • @Passatizhi I cannot do it like in the example because links are not supported when the network mode is awsvpc – Linkavich14 Jun 11 '19 at 03:26

2 Answers2

12

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

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • Thank you for your response, I tried to do it like that before but the problem is that I have to use Fargate and Fargate only allows awsvpc – Linkavich14 Jun 11 '19 at 06:52
  • Fargate did not support linking. you can check the documentation – Adiii Jun 11 '19 at 07:36
9

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.

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
Haran
  • 1,040
  • 2
  • 13
  • 26
  • 1
    Thanks for your response Haran, that is how I have them configured, after I created them and started the service, it worked fine and they were talking to each other but the next day the same happened all over again, no communication. I don't know why – Linkavich14 Jun 12 '19 at 23:23
  • 2
    Is there any reason this wouldn't work with an EC2 instance type? Also, why do you say it's not, "production grade"? – pdoherty926 Jan 11 '21 at 21:36
  • 2
    @pdoherty926 the reason is if one of the containers is unhealthy the both will be terminated, plus you can not scale one container, if one container scale the other will also scale. this is more similar to two processes in a single container. they are highly depended while the purpose of microservice is to minimize the dependency between service. – Adiii Mar 13 '21 at 04:21