Question: How can I avoid DNS resolution failures with an Amazon EC2 Linux instance?

I want to avoid DNS resolution failures with Amazon Elastic Compute Cloud (Amazon EC2) Linux instances.

Short descriptionTo decrease CPU and network usage and avoid DNS resolution failures, apply a DNS cache.If you're using a DNS cache to query external DNS resources, then the cache answers most of the recurring DNS queries locally. The cache does this without interacting with the DNS resolver over the network. Examples of external DNS resources that you can query include the following:Amazon Relational Database Service (Amazon RDS)Amazon ElastiCacheAmazon Simple Storage Service (Amazon S3)The following procedure applies to all versions of Amazon Linux. If you're using another distribution, select the documentation for your distribution from the following list:For Debian, see Local caching on the debian.org website.For Ubuntu, see Local DNS cache on the Ubuntu website.For RHEL, see How to configure DNS caching server with dnsmasq in RHEL on the Red Hat customer portal.

ResolutionSet up a local DNS cache using dnsmasqSet up a local DNS cache, using dnsmasq (a DHCP and cache DNS server). For an overview of dnsmasq, see the dnsmasq documentation.1.    Install the dnsmasq server by running the following command:sudo yum install -y dnsmasq2.    Create a dedicated system user to run dnsmasq using the following commands:sudo groupadd -r dnsmasqsudo useradd -r -g dnsmasq dnsmasqNote: dnsmasq typically runs as the root user, but drops root privileges after startup by changing to another user (by default, the user is "nobody").3.    Create a copy of the dnsmasq.conf file using the following command:sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig4.    Open the configuration file using a text editor (for example, vim):sudo vim /etc/dnsmasq.conf5.    Edit the /etc/dnsmasq.conf file so that it's similar to the following:# Server Configurationlisten-address=127.0.0.1port=53bind-interfacesuser=dnsmasqgroup=dnsmasqpid-file=/var/run/dnsmasq.pid# Name resolution optionsresolv-file=/etc/resolv.dnsmasqcache-size=500neg-ttl=60domain-neededbogus-priv6.    Create the /etc/resolv.dnsmasq file, and then set the Amazon DNS server or the custom domain-name-servers that you specified on DHCP options sets:sudo bash -c "echo 'nameserver 169.254.169.253' > /etc/resolv.dnsmasq"Note: For more information about DNS server locations, see What is DHCP? Adjust the file /etc/resolv.dnsmasq to use the name server for that network in the following scenarios:You create an AMI from an instance with the dnsmasq cache to launch in another VPC with a different CIDR.You have a custom DNS server specified in your DHCP options.7.    Restart the dnsmasq server and set the service to start up on boot using the following commands:Amazon Linux 1sudo service dnsmasq restartsudo chkconfig dnsmasq onAmazon Linux 2sudo systemctl restart dnsmasq.servicesudo systemctl enable dnsmasq.service8.    Verify that dnsmasq is working correctly using the dig command:dig aws.amazon.com @127.0.0.1If the response is similar to the following, then the dnsmasq cache 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.    Set the dnsmasq DNS cache as the default DNS resolver.Note: You must suppress the default DNS resolver provided by DHCP by changing or creating the /etc/dhcp/dhclient.conf file. For more information, see My private Amazon EC2 instance is running Amazon Linux, Ubuntu, or RHEL. How do I assign a static DNS server to the EC2 instance that persists during reboot?10.    Configure the default DNS resolver as a fallback option by using the following commands:sudo bash -c "echo 'supersede domain-name-servers 127.0.0.1, 169.254.169.253;' >> /etc/dhcp/dhclient.conf"11.    To apply the change, run the dhclient command, restart the network service, or reboot your instance:sudo dhclient-or-sudo systemctl restart network-or-sudo rebootTo verify that your instance is using the DNS cache, run the dig command:dig aws.amazon.comIf the response indicates that the server replying to your DNS request is 127.0.0.1, 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) <<<-------...Automate dnsmasqYou can use one of the following to automate the installation and configuration of dnsmasq as a DNS resolver on Amazon Linux:AutomateDnsmasq.sh bash scriptAutomateDnsmasq.cloudinit directivesIf you want to automate the dnsmasq installation on other Linux distributions, you can use either file to make the necessary customization.Both files can run on VPC instances because they use the Amazon DNS server alternative address of 169.254.169.253.You can run either file at launch time by passing the contents of the file in the user data field. You can run the Bash script as a standalone script or with an AWS Systems Manager run command to perform the actions on an instance.To run the Bash script as a standalone script:1.    Download the script on your instance and make it executable.wget https://raw.githubusercontent.com/awslabs/aws-support-tools/master/EC2/AutomateDnsmasq/AutomateDnsmasq.shchmod +x AutomateDnsmasq.sh2.    Run the following command as a root user or use sudo.sudo ./AutomateDnsmasq.sh

Related informationAmazon EC2 instance IP addressing