How to Create Dynamic DNS in AWS Route 53 Using Python and the AWS SDK
Table of contents
What Is Dynamic DNS?
In the traditional method, we create DNS records with a Domain/Subdomain name and attach it to a specific value like the IP address of the server (A Records) or another DNS name (CNAME Records). But all these records are static in the sense, that we have to manually make changes in the DNS records whenever we are required. The solution to that, we are introduced to Dynamic DNS. They are being auto-updated using some software or some API calls from function. So, no manual configuration is needed.
Why we need Dynamic DNSš¤?
It has multiple uses where your public IP address is not constant and you donāt want to change your DNS records each time your IP changes. The best use case that I found for myself is using DDNS to connect to my private server instead of using the public IP of the server to access it. you have got the idea what I am talking about, when you have a private server, you donāt want to keep it on all the time as it might increase your cost. If you turn it off and turn it on again then your server has a brand new IP, and If you are using Remote Clients to connect to the service, then you have to update the IP address to again make connection. By using, DDNS at the start of the instance, the DDNS function will update the DNS record with the new public IP address. This will make your work more organized and easy.
Dynamic DNS Service Providers (Freeš¤):
Do Route 53 have this amazing feature š?
Nopeš¤”, but we will figure something out. We can manage route 53 whole service using AWS SDK APIs. So, we will write one script using AWS SDK, which will update our DNS record with the current public IP of the service on instance boot-up. In this blog, I have mentioned all the steps to set up the DDNS service for route 53 with how to run the script on boot up and set cron to run script at every 5 min.
So, How do we do it š¼?
Letās start the process with creating Hosted Zone.
- Create Route 53 Hosted Zone of your domain and note the Hosted Zone ID.
2. I assume you have already created your EC2 instance or noted the IP address of your required device. Create DNS record of your choice and set the value.
3. You need to create one IAM user which have permissions to edit the route 53 records. Here I am using user with Route 53 full permissions.
Permission Polices Name: AmazonRoute53FullAccess
4. Generate the Access Keys of that user from Security Credentials, which is required for your script to run perfectly.
5. Now SSH to your server, here I am using Ubuntu server for this to implement. Create new directory and create new python file.
Commands:
sudo apt update
mkdir ddns && cd ddns
nano ddns.py
6. Copy the ddns.py script file from the following GitHub Repository:
https://github.com/nishant-p-7span/
Paste it to the ddns.py file on your server.
Replace hosted zone id, domain name, access key and Secrete access key with your actual credentials.
7. Before we run the command we need to install python libraries boto3 and requests.
sudo apt install python3-boto3
sudo apt install python3-requests
8. Give ddns.py file 777 permission.
sudo chmod +x ddns.py
9. Run the code to check whether it is working fine or not.
python3 ddns.py
You might see some response like this:
How to run this code on instance boot upš?
- Create systemd service file:
sudo nano /etc/systemd/system/update-dns.service
2. Paste the following code to the file:
[Unit]
Description=Update Route 53 DNS with Public IP
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/ubuntu/ddns/ddns.py
WorkingDirectory=/home/ubuntu/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=ubuntu
[Install]
WantedBy=multi-user.target
3. Enable this service process and set up to run on boot.
sudo systemctl daemon-reload
sudo systemctl enable update-dns.service
sudo systemctl start update-dns.service
4. Now, stop the instance, and start it again. After few seconds you can observe that your IP address is updated from the route 53 DNS records.
5. Now you can use this DNS name on your remote clients to connect to the server and at each bootup it will automatically update the IP with newer one.
How to run this script every 5 min?
For this we will set up cron job, which will be executed at every 5 min.
- Edit crontab file:
crontab -e
2. Add following commands to the files:
*/5 * * * * /usr/bin/python3 /home/ubuntu/ddns/update_dns.py
3. Enable and start crontab.
sudo systemctl enable cron
sudo systemctl start cron
4. Letās check if it is working or not.
sudo systemctl status cron
How Useful it is?
Now, your Dynamic DNS is ready for Route 53. You can now use this domain to connect to your instance with Remote Clients. If you have some application running on the server with SSL, you can use this even after your IP is changed, your SSL will still work perfectly. This method will bring down your efforts to manually change DNS records each time your server got a new IP address.
References:
More
An interesting read? Here is more related to it.
We Make pixel perfect things
We Make pixel perfect things
We Make pixel perfect things
We Make pixel perfect things
We Make pixel perfect things
We Make pixel perfect things
India (HQ)
201, iSquare Corporate Park, Science City Road, Ahmedabad-380060, Gujarat, India
For Sales
[email protected]
Looking For Jobs
Apply Now