Skip to content

Instantly share code, notes, and snippets.

@arbabnazar
Last active February 4, 2020 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arbabnazar/9ad851adf8965e3921acc44a517a9295 to your computer and use it in GitHub Desktop.
Save arbabnazar/9ad851adf8965e3921acc44a517a9295 to your computer and use it in GitHub Desktop.

How to use this script:

route53-zone-migrate.sh -s aws_source_profile -t aws_target_profile -d domain_name

Help

 -s,    Uses a AWS CLI profile name for Route53 zone source account.
 -t,    Uses a AWS CLI profile name for Route53 zone target account.
 -d,    Domain name that contains the resource record sets that you want to migrate.
#!/usr/bin/env bash
set -eo pipefail
usage()
{
echo " Usage: ${0} -s aws_source_profile -t aws_target_profile -d domain_name
-s, Uses a AWS CLI profile name for Route53 zone source account.
-t, Uses a AWS CLI profile name for Route53 zone target account.
-d, Domain name that contains the resource record sets that you want to migrate.
"
}
CYAN='\033[1;36m'
GREEN='\033[1;32m'
RED='\033[0;31m'
NC='\033[0m'
while getopts ":s:t:d:h" opt; do
case ${opt} in
s) SOURCE_PROFILE="${OPTARG}"
;;
t) TARGET_PROFILE="${OPTARG}"
;;
d) DOMAIN_NAME="${OPTARG}"
;;
h) usage && exit 1
;;
\?) echo "Invalid option -${OPTARG}" >&2
usage
;;
esac
done
array=( "jq" "aws" )
for i in "${array[@]}"; do
command -v $i >/dev/null 2>&1 || output "$i is required but not installed."
done
echo -e "${GREEN}[INFO]${NC} Fetching the hosted zone id for ${GREEN}${DOMAIN_NAME}${NC} from source and target AWS accounts"
source_hosted_zone_id=$(aws --profile ${SOURCE_PROFILE} route53 list-hosted-zones --query 'HostedZones[?Name==`'${DOMAIN_NAME}'.`].Id' --output text)
target_hosted_zone_id=$(aws --profile ${TARGET_PROFILE} route53 list-hosted-zones --query 'HostedZones[?Name==`'${DOMAIN_NAME}'.`].Id' --output text)
echo -e "${GREEN}[INFO]${NC} Fetching the resource record sets of ${GREEN}${DOMAIN_NAME}${NC} domain from ${CYAN}${SOURCE_PROFILE}${NC} AWS account"
route53_records=$(aws --profile ${SOURCE_PROFILE} route53 list-resource-record-sets --hosted-zone-id ${source_hosted_zone_id} | jq '.ResourceRecordSets
| {"Changes":[.[] | select(.Type!="SOA") | select(.Type!="NS") | {"Action":"CREATE","ResourceRecordSet":.}]}')
echo -e "${RED}[Warn]${NC} Updating the resource record sets of ${GREEN}${DOMAIN_NAME}${NC} domain on ${CYAN}${TARGET_PROFILE}${NC} AWS account"
aws --profile ${TARGET_PROFILE} route53 change-resource-record-sets --hosted-zone-id ${target_hosted_zone_id} --change-batch "${route53_records}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment