Automating DNS Record Updates with AWS Route 53 and Bash Scripting

In today’s dynamic and cloud-centric world, managing DNS records is a crucial aspect of maintaining an online presence. AWS Route 53 provides a reliable and scalable DNS web service that allows users to route traffic to various AWS resources. In this article, we will explore how to automate the process of updating DNS records using a Bash script.

The Scenario

Consider a scenario where you need to update multiple DNS records for the domain “yourwebsite.com.au” in AWS Route 53. The records include MX, TXT, CNAME, and DMARC records. To accomplish this task efficiently, we will leverage a Bash script that interacts with the AWS Command Line Interface (CLI).

The DNS Records JSON

Firstly, let’s define the DNS records in a structured JSON format. This JSON file, named dns_changes.json, contains an array of changes, each specifying the action (UPSERT), the resource record set details (such as Name, Type, TTL), and the associated resource records.

{
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "yourwebsite.com.au",
        "Type": "MX",
        "TTL": 300,
        "ResourceRecords": [{"Value": "10 mailserver.purelymail.com."}]
      }
    },
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "yourwebsite.com.au",
        "Type": "TXT",
        "TTL": 300,
        "ResourceRecords": [
          {"Value": "\"v=spf1 include:_spf.purelymail.com ~all\""},
          {"Value": "\"purelymail_ownership_proof=[your ownership proof]\""}
        ]
      }
    },
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "purelymail1._domainkey.yourwebsite.com.au",
        "Type": "CNAME",
        "TTL": 300,
        "ResourceRecords": [{"Value": "key1.dkimroot.purelymail.com."}]
      }
    },
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "purelymail2._domainkey.yourwebsite.com.au",
        "Type": "CNAME",
        "TTL": 300,
        "ResourceRecords": [{"Value": "key2.dkimroot.purelymail.com."}]
      }
    },
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "purelymail3._domainkey.yourwebsite.com.au",
        "Type": "CNAME",
        "TTL": 300,
        "ResourceRecords": [{"Value": "key3.dkimroot.purelymail.com."}]
      }
    },
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "_dmarc.yourwebsite.com.au",
        "Type": "CNAME",
        "TTL": 300,
        "ResourceRecords": [{"Value": "dmarcroot.purelymail.com."}]
      }
    }
  ]
}

The Bash Script

Now, let’s create a Bash script named update_dns.sh to automate the DNS record updates. The script uses the AWS CLI to communicate with AWS Route 53.

#!/bin/bash

# Set your AWS CLI profile and hosted zone ID
HOSTED_ZONE_ID="your zone id (from aws route 53)"
DOMAIN_NAME="yourwebsite.com.au"  # Replace with your actual domain name

# Function to update DNS records
update_record() {
    aws route53 change-resource-record-sets \
        --hosted-zone-id "$HOSTED_ZONE_ID" \
        --change-batch "file://dns_changes.json"
}

# Call the function to update DNS records
update_record

In this script, the update_record function reads the DNS changes from the dns_changes.json file and updates the records in the specified hosted zone.

Conclusion

Automating DNS record updates with a Bash script and AWS Route 53 can save time and reduce manual errors. By utilizing the AWS CLI and a well-structured JSON file, you can easily manage and modify DNS records to adapt to changing requirements.

Feel free to customize the script and JSON file according to your specific DNS records and hosted zone details. Automation simplifies repetitive tasks, allowing you to focus on more strategic aspects of your infrastructure.

Remember to securely store and manage any sensitive information, such as AWS credentials, and consider incorporating this script into your deployment or update workflows for a seamless experience.


Posted

in

by

Comments

Leave a Reply

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