I would like to elaborate @Imran detailed answer a bit more, since, most of the answer talks about SRV
DNS Record Type and showing Nginx example only for a premium version of Nginx ( and SRV
).
In case you work with ECS Fargate and configured A
DNS Record. the most important thing is to configure a proper resolver
.
From the docs:
Configures name servers used to resolve names of upstream servers into
addresses, for example:
resolver 127.0.0.1 [::1]:5353;
The address can be specified as a
domain name or IP address, with an optional port. If port is not
specified, the port 53 is used. Name servers are queried in a
round-robin fashion.
with that been said the resolver must resolve the Private DNS. therefore, we need to use the NS
DNS Record.
using 8.8.8.8
as a resolver won't work since this DNS can't resolve the Private DNS.
NS stands for ‘name server’ and this record indicates which DNS server
is authoritative for that domain (which server contains the actual DNS
records). A domain will often have multiple NS records which can
indicate primary and backup name servers for that domain.
In order to get the DNS Resolver run the following command:
aws route53 list-resource-record-sets --hosted-zone-id %HOSTED_ZONE_ID% --query "ResourceRecordSets[?Type == 'NS']"
Pick one of the resource records and place it into the Nginx resolver
(including the trailing .
).
Nginx basic template:
events {
worker_connections 768;
}
http {
# DNS Resolver
resolver ns-###.awsdns-####.com. valid=10s;
gzip on;
gzip_proxied any;
gzip_types text/plain application/json;
gzip_min_length 1000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
server {
listen 80;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# This is the important part
proxy_pass http://ecs-fargate-svc.local:8080;
}
location = /health-check {
return 200 'all good';
}
}
}
Few points that need to consider:
- Don't forget to add the mapping port (in my example
8080
).
- Make sure the Security group allows traffic within the VPC.
- Since working with Fargate and we have limited logs, consider creating an EC2 instance in the VPC the ECS Fargate tasks located and try to curl\ping the URL\DNS Record.
My service discovery:

Documentations:
Nginx resolver
The name server (NS) record