Search code examples
nginxamazon-ecs

ECS Nginx network setup


I have 3 containers on ECS: web, api and nginx. Basically nginx is proxying traffic to web and api containers:

upstream web {
  server web-container:3000;
}

upstream api {
  server api-container:3001;
}

But every time I redeploy web or api they change their IPs so I need to redeploy nginx afterwards in order to make it to "pick up" new IPs.

Is there a way to avoid this so I could just update let's say api service and nginx service would automatically proxy to correct IP address?


Solution

  • I assume these containers belong to 3 different task definitions and ultimately 3 different tasks (or better 3 different services).

    If that is the setup then you want to use service discovery for this. This only works with ECS services and the idea is that you create 3 distinct services each with 1+ tasks in it. You give the service a name (e.g. nginx, web, api) and each container in them is going to be able to resolve the other containers by pointing to the fqdn (e.g. api.local). When your container in the nginx service tries to connect to api.local service discovery will resolve that name to the IP of one of the tasks in the ECS service api.

    If you want to see an example re how this is setup you can look at this demo app and particularly at this CloudFormation template