How to create record in route 53 using boto3

aws
Reading Time: 3 minutes

Hello Readers!!! In this artical, we will see how we can create a record in route 53 using boto3. So, first we will see what route 53 is. Route 53 is a highly available and scalable DNS web service.It gives businesses and developers a cost-effective way to route end users to internet applications by translating the domain names into IP addresses. we need route 53 because of its simple routing policy which means that a single resource performs a given function for your domain.

Steps to create hosted zone in route 53:

We can create hosted zone in route 53 by following the below steps:

Step 1: First log into aws management console and search for route 53.

Step 2: It will redirect to route 53 dashboard. Now, click on hosted zones.

Step 3: Then click on create hosted zone.

Step 4: In the below figure, you will see hosted zone configuration. Here you have to enter your domain name. In type, we will select public hosted zone as i want to route traffic on the internet if you want to route traffic in a vpc, then you can select private one. You can also add tags here.

Step 5: In hosted zone you can see that the type will be public and it is created by route 53 directly. And you should have two records count.

Step 6: When you click on the hosted zone, you are going to have the ns records and the soa records.

Steps to create record using boto3:

Step 1: For creating record in route 53 using boto3, first we will click on domain name.

Step 2: Now, a new page will open that define the hosted zone detail.

Step 3: We will click on hosted zone details and copy the hosted zone id.

Step 4: Now we will open the code editor and paste the below command and in place of hosted zone id we will give our hosted zone id that we copied previously and in place of domain we will give our domain name.

from pydoc import cli
import boto3
import os,time
import json
import logging
import secrets
import string

client =boto3.client('route53',
    aws_access_key_id=os.environ.get('AWS_ACCESS_KEY'),
    aws_secret_access_key=os.environ.get('AWS_SECRET_KEY'))
    
def create_Record(platform,public_ip):
    length=8
    domain="route_53.com"
    result_str = res = ''.join(secrets.choice(string.ascii_uppercase + string.digits)for i in range(length))
    record_name= platform + "-" + result_str + '.' + domain

    client.change_resource_record_sets(
    ChangeBatch={
            'Changes': [
                {
                    'Action': 'CREATE',
                    'ResourceRecordSet': {
                    'Name':record_name,
                    'ResourceRecords': [
                        {
                            'Value': '127.0.0.1' ,
                        },
                    ],
                    'TTL': 60,
                    'Type': 'A',
                },
            },
        ],
    },
    HostedZoneId='Z04582922T8MEA4QE4L9A',
)
if __name__ == "__main__":
    create_Record('jenkins','127.0.0.1')

save this file as route53.py and run the below command:

python3 route53.py

It will give our hosted zone id as an output.

Conclusion:

In conclusion, we saw that how we can create hosted zone and records in route 53 using boto3.

knoldus

Written by 

I am reliable, hard-working with strong attention to detail and eager to learn about new technologies and business issues. I am able to work well both on my own initiative and as part of a team.

Leave a Reply