Automated DNS Record Creation with AWS Route 53 and Bash Script
This is an useful script for automating the process of creating DNS records using the AWS Route 53 service.
First, there's a list of items in the format "name, domain, IP, hosted zone ID". Each item represents a DNS record to be created.
Bash Script:
Let me break it down for you:
- Then, there's a Bash script starting with
#!/bin/bash
, indicating it's a Bash script. - Inside the script, there's a
for
loop iterating over each line of the file/tmp/list
. It's using backticks to execute thecat /tmp/list
command and iterate over its output. - For each line in the list, it's executing the AWS CLI command
aws route53 change-resource-record-sets
to create a DNS record. - The
aws route53 change-resource-record-sets
command is being supplied with parameters:--hosted-zone-id
: The hosted zone ID extracted from the input line.--change-batch
: A JSON string specifying the changes to be made. This JSON includes:- A comment.
- An array of changes, each containing:
- The action (in this case, "CREATE").
- Details of the resource record set to be created, including:
- Name: Constructed from the name and domain fields of the input line.
- Type: Always "CNAME" in this script.
- TTL: Time to Live for the DNS record.
- ResourceRecords: An array containing the IP address extracted from the input line.
- Inside the JSON, the values for name, type, TTL, and IP address are extracted from the input line using
cut
commands. - The script repeats this process for each line in the list, effectively creating a DNS record for each item in the list.
This script essentially automates the creation of DNS records based on the information provided in the list file.