Bobcares

How to Avoid DNS resolution failures in EC2

by | Jul 12, 2021

Wondering how to avoid DNS resolution failures in EC2? We can help you!

Here at Bobcares, we handle requests from our customers to handle DNS resolution failure issues as a part of our Server Management Services.

Today let’s see how our Support Engineers do this for our customers with EC2 instances.

How to Avoid DNS resolution failures in EC2

Generally, to decrease CPU, network usage and avoid DNS resolution failures we can apply a DNS cache.

However, when we use a DNS cache to query external DNS resources, the cache will answer most of the recurring queries locally without interacting with the DNS resolver over the network.

Following are some of the external DNS resources in AWS:

  • Amazon Relational Database Service (Amazon RDS)
  • The Amazon ElastiCache
  • Amazon Simple Storage Service (Amazon S3)

We can fix this with the help of dnsmasq.

Steps to setup local DNS cache, using dnsmasq

Now we will see how our Support Engineers set up a local DNS cache, using dnsmasq (a DHCP and cache DNS server).

1.  Firstly, we can install the dnsmasq server by running the following command:

sudo yum install -y dnsmasq

2.  Then we can create a dedicated system user to run dnsmasq using the following commands:

sudo groupadd -r dnsmasq
sudo useradd -r -g dnsmasq dnsmasq

Note: dnsmasq typically runs as the root user, but drops root privileges after startup by changing to another user. And by default, the user is “nobody”.

3. Next, we will make a copy of the dnsmasq.conf file using the following command:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

4.  And open the configuration file using a text editor as given below:

sudo vim /etc/dnsmasq.conf

5.  Now we will edit the /etc/dnsmasq.conf file so that it is similar to the following:

# Server Configuration
listen-address=127.0.0.1
port=53
bind-interfaces
user=dnsmasq
group=dnsmasq
pid-file=/var/run/dnsmasq.pid

# Name resolution options
resolv-file=/etc/resolv.dnsmasq
cache-size=500
neg-ttl=60
domain-needed
bogus-priv

6. After that we will create the /etc/resolv.dnsmasq file, and set the Amazon DNS server or the custom domain-name-servers that are specified on DHCP options sets.

sudo bash -c "echo 'nameserver 169.254.169.253' > /etc/resolv.dnsmasq"

7. We can restart the dnsmasq server and set the service to start up on boot using the following commands:

Amazon Linux 1

sudo service dnsmasq restart
sudo chkconfig dnsmasq on

Amazon Linux 2

sudo systemctl restart dnsmasq.service
sudo systemctl enable dnsmasq.service

8. Then we can verify that dnsmasq is working correctly using the dig command given below:

dig aws.amazon.com @127.0.0.1

If the result is similar to the following, then it is working correctly:

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.56.amzn1 <<>> aws.amazon.com @127.0.0.1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25122
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;aws.amazon.com.            IN    A

;; ANSWER SECTION:
aws.amazon.com.        41    IN    A    54.239.31.69

;; Query time: 1 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
...

9. After verification we can set the dnsmasq DNS cache as the default DNS resolver.

10. We can configure the default DNS resolver as a fallback option by using the following:

sudo bash -c "echo 'supersede domain-name-servers 127.0.0.1, 169.254.169.253;' >> /etc/dhcp/dhclient.conf"

11. And we can either run the dhclient command or reboot the instance to apply the change:

sudo dhclient

OR

sudo reboot

12. To verify that the instance is using the DNS cache, we can run the dig command:

dig aws.amazon.com

If we get the following reply then the DNS cache is working correctly:

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.56.amzn1 <<>> aws.amazon.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1028
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;aws.amazon.com.            IN    A

;; ANSWER SECTION:
aws.amazon.com.        55    IN    A    54.239.31.69

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) <<<-------
...

Steps to automate the installation and configuration of dnsmasq as a DNS resolver 

We can use one of the following to automate the installation and configuration of dnsmasq as a DNS resolver on Amazon Linux:

  • AutomateDnsmasq.sh Bash script
  • AutomateDnsmasq.cloudinit directives

Both the above files can run on VPC instances or EC2-Classic since they use the Amazon DNS server alternative address of 169.254.169.253 for VPC and 172.16.0.23 for EC2-Classic.

We can run either file at launch time by passing the contents of the file in the user data field. Moreover, we can run the Bash script as a standalone script or with an AWS Systems Manager run command to perform the actions on an existing instance.

For running the Bash script as a standalone script we must do the following:

1. Firstly, we have to download the script on our instance and make it executable.

For this, we can use the following command:

wget https://raw.githubusercontent.com/awslabs/aws-support-tools/master/EC2/AutomateDnsmasq/AutomateDnsmasq.sh
chmod +x AutomateDnsmasq.sh

2.  After that we can run the following command as a root user or use sudo.

sudo ./AutomateDnsmasq.sh

[Need assistance? We can help you]

Conclusion

To conclude, we saw the steps that our Support Techs follow to Avoid DNS resolution failures in EC2.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF