Configure EC2 instances to selectively bypass default DNS resolver in all regions

,

I have a network of instances. Most of them are using the default VPC’s DNS resolver to prevent exfiltration and to resolve internal domains (using a custom TLD). That all works fine.

However, there is one (Ubuntu) instance that needs to use a general, public resolver to reach the Internet at large (e.g. 8.8.8.8)

I managed to get this work in us-east-1 by replacing /etc/systemd/resolved.conf with

[Resolve]
DNS=8.8.8.8 208.67.222

and adding an /etc/systemd/resolved.conf.d/MYTLD.conf

[Resolve]
Domains=MYTLD
DNS=10.6.0.2

After restarting systemd-resolved, the instance resolves all domains just fine.

But when I apply this exact same configuration to a nigh-identical network in us-east-2, public DNS doesn’t work.

One suggestion I found on StackExchange was to symlink /etc/resolv.conf to /run/systemd/resolve/resolv.conf, which fixed public DNS but broke the internal

Can anyone provide any insight on how to fix this?


Related discussions:

You do know the VPC resolver does resolve public DNS records by default right?

Yes, but we’ve set it to default deny for this VPC. All the other instances only need to be able to resolve a small allow-list of known domains; basically just the mail server needs to be able to access arbitrary ones.

Okay gotcha. Are you sure the traffic can get to the internet? You have the correct routes, NACLs, etc? Might try using the VPC Reachability Analyzer to double check if you’re certain the OS DNS configuration is correct.

Yes, I can manually query the desired server and get results: e.g.

nslookup google.com 8.8.8.8

If you look at VPC flow logs, do you see your queries going to the right place? Does your manual test and your configured test produce the same flow log results?

VPC configuration isn’t the problem then, if you can resolve from the command line. Are the editions of Ubuntu the same? Split DNS is not a feature that all editions of Ubuntu have

Oh! Good catch.

The one that works is U20; the one that doesn’t is U16. Alright, I will have to follow up with my team about the upgrade timeline

Belay that. I was looking at the wrong terminal. Both are 20.04

So I’ve never messed with flow logs before. Is there a way to filter them by port or anything? I’m really not interested in any queries outside port 53.

That said,

if

nslookup google.com 8.8.8.8

Succeeds and

nslookup google.com

fails, that means that something is going wrong on the EC2 side, right? The latter is clearly not sending a DNS query to 8.8.8.8 despite what my resolved.conf says.

https://gist.github.com/bfgits/07b22617c53042a4cf74500b9b9c24c4

Flowlogs can be searched for “53” or “a.b.c.d” though I’d probably start with a TCP dump and see what the traffic is doing locally first.

I did try searching 53,but that’s unfortunately too generic a string to meaningfully filter results. (Even on a test environment, it’s still a lot of hosts generating a lot of traffic)

Try cloudwatch logs insights if you want more granularity with filtering

It’s been a minute since I’ve played with these, but couldn’t you just hack it out by manually editing your resolv.conf, adding the dns entries, then doing a “chattr +i /etc/resolv.conf” to make it immutable so no process can write to it?

Not sure if it’d do more harm than good, but might be worth a try.

Ok, I figured out tracing the packets with tcpdump from the instance itself…

So the box itself is getting back results:

12:00:00.000000 IP > 10.6.2.179.port > 8.8.8.8.53: 1111+ [1au] A? bing.com. (37)
12:00:00.000040 IP > 10.6.2.179.port > 10.6.0.2.53: 2222+ [1au] A? bing.com. (37)
12:00:00.000150 IP > 10.6.0.2.53 > 1.6.2.179.port: 2222 0/0/0 (37)
12:00:00.000450 IP > 8.8.8.8.56 > 10.6.2.179.port: 1111 2/0/1 A 13.107.21.200, A 204.79.97.200 (69)

But the nslookup tool itself is still saying “Can’t find bing.com: No answer”

I found it. I had my resolver returning BLOCK > NODATA for domains that weren’t in the allow-list. NODATA responses are considered a successful hit by systemd-resolve’s LLMNR system, so that created a race condition between the resolver and the public DNS.

Setting the resolver to default to BLOCK > NXDOMAIN fixed it.