Skip to content

Instantly share code, notes, and snippets.

@ruzickap
Created May 6, 2020 06:30
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 ruzickap/95eb1b0eb1d0d424c668cd3bb4a43033 to your computer and use it in GitHub Desktop.
Save ruzickap/95eb1b0eb1d0d424c668cd3bb4a43033 to your computer and use it in GitHub Desktop.
AWS Route53 subdomain delegation
# You have defined the "cna.example.cloud" zone in Route53
# You want to create subdomain "${USER}-k8s.cna.example.cloud" and configure proper delegation form parrent zone
echo "* Create DNS zone ${USER}-k8s.cna.example.cloud"
aws route53 create-hosted-zone --output json \
--name ${USER}-k8s.cna.example.cloud \
--caller-reference "$(date)" \
--hosted-zone-config="{\"Comment\": \"Created by ${USER}@example.com\", \"PrivateZone\": false}" | jq
echo "* Get the NS servers from the new zone ${USER}-k8s.cna.example.cloud"
NEW_ZONE_ID=$(aws route53 list-hosted-zones --query "HostedZones[?Name==\`${USER}-k8s.cna.example.cloud.\`].Id" --output text)
NEW_ZONE_NS1=$(aws route53 get-hosted-zone --output json --id ${NEW_ZONE_ID} --query "DelegationSet.NameServers" | jq -r '.[0]')
NEW_ZONE_NS2=$(aws route53 get-hosted-zone --output json --id ${NEW_ZONE_ID} --query "DelegationSet.NameServers" | jq -r '.[1]')
NEW_ZONE_NS3=$(aws route53 get-hosted-zone --output json --id ${NEW_ZONE_ID} --query "DelegationSet.NameServers" | jq -r '.[2]')
NEW_ZONE_NS4=$(aws route53 get-hosted-zone --output json --id ${NEW_ZONE_ID} --query "DelegationSet.NameServers" | jq -r '.[3]')
echo '* Create the NS record in "cna.example.cloud" for proper zone delegation'
ORIG_ZONE_ID=$(aws route53 list-hosted-zones --query "HostedZones[?Name==\`cna.example.cloud.\`].Id" --output text)
cat << EOF | aws route53 change-resource-record-sets --output json --hosted-zone-id ${ORIG_ZONE_ID} --change-batch file:///dev/stdin | jq
{
"Comment": "Create a subdomain NS record in the parent domain",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "${USER}-k8s.cna.example.cloud",
"Type": "NS",
"TTL": 30,
"ResourceRecords": [
{
"Value": "${NEW_ZONE_NS1}"
},
{
"Value": "${NEW_ZONE_NS2}"
},
{
"Value": "${NEW_ZONE_NS3}"
},
{
"Value": "${NEW_ZONE_NS4}"
}
]
}
}
]
}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment