- 
      
 - 
        
Save ybudimirov/bc7d14d2cb425d134998d030c79b43c6 to your computer and use it in GitHub Desktop.  
    CLI to add DNS Records in Route53
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/bash | |
| ## Allows for creation of "Basic" DNS records in a Route53 hosted zone | |
| ## set some defaults if variables haven't been overridden on script execute | |
| zone_name=${zone_name:-example.com.} | |
| action=${action:-UPSERT} | |
| record_type=${record_type:-A} | |
| ttl=${ttl:-300} | |
| wait_for_sync=${wait_for_sync:-false} | |
| # echo $zone_name | |
| # echo $action | |
| # echo $record_type | |
| # echo $ttl | |
| # echo $wait_for_sync | |
| # echo $1 | |
| # exit | |
| function main() { | |
| record_name=$1 | |
| record_value=$2 | |
| [[ ! -z $record_name ]] && echo "record_name is: $record_name" || exit 1 | |
| [[ ! -z $record_value ]] && echo "record_value is: $record_value" || exit 1 | |
| change_id=$(submit_resource_record_change_set) || exit 1 | |
| echo "Record change submitted! Change Id: $change_id" | |
| if $wait_for_sync; then | |
| echo -n "Waiting for all Route53 DNS to be in sync..." | |
| until [[ $(get_change_status $change_id) == "INSYNC" ]]; do | |
| echo -n "." | |
| sleep 5 | |
| done | |
| echo "!" | |
| echo "Your record change has now propogated." | |
| fi | |
| # echo 'Thank you for using "The Cloud".' | |
| } | |
| function change_batch() { | |
| jq -c -n "{\"Changes\": [{\"Action\": \"$action\", \"ResourceRecordSet\": {\"Name\": \"$record_name\", \"Type\": \"$record_type\", \"TTL\": $ttl, \"ResourceRecords\": [{\"Value\": \"$record_value\"} ] } } ] }" | |
| } | |
| function get_change_status() { | |
| aws route53 get-change --id $1 | jq -r '.ChangeInfo.Status' | |
| } | |
| function hosted_zone_id() { | |
| aws route53 list-hosted-zones | jq -r ".HostedZones[] | select(.Name == \"${zone_name}\") | .Id" | cut -d'/' -f3 | |
| } | |
| function submit_resource_record_change_set() { | |
| aws route53 change-resource-record-sets --hosted-zone-id $(hosted_zone_id) --change-batch $(change_batch) | jq -r '.ChangeInfo.Id' | cut -d'/' -f3 | |
| } | |
| function usage() { | |
| echo "usage: $0 <record_name> <record_value>" | |
| echo "" | |
| echo "possible env config settings and their defaults:" | |
| echo " - zone_name=$zone_name" | |
| echo " - action=$action" | |
| echo " - ttl=$ttl" | |
| echo " - record_type=$record_type" | |
| echo " - wait_for_sync=$wait_for_sync" | |
| echo " | |
| Example for multiple domains: | |
| cat list_of_domains|while read recordname; do record_type=CNAME scripts/add-dns-record.sh $recordname sub.example.com; done | |
| " | |
| echo "" | |
| } | |
| if [ "$#" -eq "0" ]; | |
| then | |
| usage; | |
| exit 0 | |
| fi | |
| main $1 $2 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment