How to create hosted zone and records in route53 using boto3

aws
Reading Time: 4 minutes

Hello folks. In this blog, I’ll explain what is route53, why we need it and after that, we’ll see how to create hosted zone and records in route53 using boto3. refer to this for a basic understanding of Amazon route52 web service.

What is route53?

Amazon route 53 is a DNS service that gives developers an efficient way to connect users to internet applications without any downtime.

In other words, we can say that Amazon route 53 is a highly available and scalable Domain Name System web service.

Route 53 has three main functions:

1 if a website needs a name, route 53 registers the name for the website (domain name)

2. Route 53 helps to connect to the browser with the website or web application when the user enters the domain name.

3. it checks the health of the resources by sending automated requests over the internet to a resource

Features of route53:

Highly reliable: ensures a consistent ability to route application
Highly scalable: automatically handles large queries without the user interaction so you don’t have to scale up/down
Easy to use: easy to set up, configure DNS settings, and provides fast response to queries
Cost-effective: Pay only for the services used
Secure: the user secures the access rights by integrating route53 with AWS(IAM)

Types of Routing Policy:

To understand the routing policies you need to know the records so records are like entries in the hosted zone. so we can say that A hosted zone is a container for records, and records contain information about how you want to route traffic for a specific domain.

while creating the records you choose the routing policy which determines how route53 responds to your query. or how the DNS query will respond?

Simple routing:
  • it allows configuring DNS with no special route 53 routing
  • It routes traffic to a single resource, e.g webserver to a website
  • With simple routing, multiple records with the same name can not be created but multiple values 
  • Can be specified in the same record.
Failover routing:
  • Failover routing routes traffic to a resource when the resource is healthy or to a different resource when the previous resource is unhealthy.
  • The records can route traffic to anything from an amazon s3 bucket as a website to a complex 
  • Tree of records.
Geolocation routing:
  • Geolocation routing routes the resources that are based on the geographical location of the users.
  • However, It localizes the content and presents part or the entire website in the language of the user
  • Geographical locations are specified by continent, country, or by the state in the united states
Geoproximity routing:
  • It routes the traffic to the resources based on the geographic location of users and their resources.
  • there is an option to route more traffic or less to a given resource by specifying a value known as bias.
  • However, A bias expands or shrinks the size of the geographic region from which traffic is routed to a resource
Latency Based routing:
  • If a website has to be installed or hosted across multiple AWS regions then, a routing policy is used.
  • it improves performance for the users by serving their requests from the AWS region that provides
  • the lowest latency
  • to use latency based routing, we should create latency records for the resources in multiple AWS regions
MultiValue answer routing:
  • it configures route53 to return multiple values in response to DNS queries
  • it also checks the health of resources and returns the multiple values only for the healthy resources
  • Similarly, it has the ability to return multiple health-checkable IP addresses to improve availability and load balancing
Weighted routing:
  • it routes multiple resources with a single domain name or subdomain name and controls the traffic is routed to each resource
  • it is useful load balancing and testing new versions of software

create the hosted zone and set records using boto3

For instance, we’ll create a function for registering a record in route53 function would accept two parameters, platform, public_ip
example : A record : create_route_53_entry(platform,public_ip)
result : A record : Jenkins-kjabsckj.<domain.com> = public_ip
record = “{0}-{1}-{3}”.format(platform,random,<MAIN_DOMAIN>)

import boto3
import string
import random
route = boto3.client('route53')


def create_hosted_zone_and_records (platform,public_ip):
  
    response = route.create_hosted_zone(
    Name='hands-on.route',
    CallerReference='hzr0001',
    )
    zones = route.list_hosted_zones_by_name(DNSName='hands-on.cloud')
    zone_id = zones['HostedZones'][0]['Id']
    print("hostedzone id :.{}".format(zone_id))

    N = 7
    res = ''.join(random.choices(string.ascii_uppercase +
                             string.digits, k = N))
    platform = platform+str(res)

    response = route.change_resource_record_sets(
        HostedZoneId=zone_id,
        ChangeBatch={
            "Comment": "Automatic DNS update",
            "Changes": [
                {
                    "Action": "CREATE",
                    "ResourceRecordSet": {
                        "Name": platform+'.'+'hands-on.route',
                        'SetIdentifier': 'set-01',
                        "Type": "A",
                        "Region": "ap-south-1",
                        "TTL": 60,
                        "ResourceRecords": [
                            {
                                "Value": public_ip,
                            },
                        ],
                    }
                },
            ]
        }
    )

create_hosted_zone_and_records('jenkins','3.128.45.9')

save it as rout53.py and After that, execute the below command

python3 route53.py

so it will print the id of the created hosted zone

How to create hosted zone and records in route53 using boto3
How to create hosted zone and records in route53 using boto3

Note: you have to delete the record before deleting the hosted zone.

How to create hosted zone and records in route53 using boto3

similarly, delete the hostedzone manually.

Conclusion:

Therefore, In this blog, we’ve learned about route53 and also how we can create hosted zone and records by using boto3. If you find this blog helpful do share it with your friends.

Written by 

A curious DevOps Intern , love to learn and working on technical skills/tools, know the basics of Linux, Docker, Ansible , Kubernetes and more..

Leave a Reply