The script takes 2 arguments as input to the script.
DNSFILE: CSV file containing the DNS records in csv format. More details on this later
DOMAINNAME: Domain name for which Hosted zone and DNS records are to be created.
Other variables:
TIMESTAMP: Unique timestamp to create a hosted zone. The one I have used is in epoch format.
MXCOUNT: To calculate the number of MX records present in CSV file. Used in functions later.
The above code will do the following in series of steps:
Create Hosted Zone and store the ID in HOSTEDZONE_ID variable.
For each line in the DNSFILE file, call the corresponding function to create the record. Currently, only A, CNAME and MX records can be updated by the script. For other’s it will be updated later on Github itself.
Since AWS change-resource-record-sets command accepts JSON, so I had to create a JSON in a variable to use it. The function takes 2 arguments as input.
mxrecfunc() { if [[ "$MXCOUNT" == 1 ]]; then addMXrecord "$DNSNAME""$DNSVALUE" sleep 5 else addMultipleMXrecord sleep 5 fi }
Updating MX records is tricky. The values can be 1 or more than 1. For DNS with single MX record is easy. However, updating with 2 MX records cause replacing of 1st MX with the 2nd one.
Here, we are using 3 functions addMXrecord, addMultipleMXrecord and mxrecfunc.
Here, the code gets a little nasty. For multiple MX values, ResourceRecords require Key Value in JSON format. So to get the code in JSON, I had to use a small python snippet to get values in array.
End result:
1 2 3 4 5 6 7 8
[ { "Value": "1 A" }, { "Value": "2 B" } ]
or
1
[{"Value":"1 A"},{"Value":"2 B"}]
Since the above have 2 values, it can be used to update MX records easily.
mxrecfunc
1 2 3 4 5 6 7 8 9
mxrecfunc() { if [[ "$MXCOUNT" == 1 ]]; then addMXrecord "$DNSNAME""$DNSVALUE" sleep 5 else addMultipleMXrecord sleep 5 fi }
The above function simply checks for the count of MX records in DNS file. If the count is 1, then addMXrecord is called, else addMultipleMXrecord.