Last active
October 30, 2024 19:07
-
-
Save leonardobiffi/34aeab7429b8d7718857a81742ec1a03 to your computer and use it in GitHub Desktop.
Export Route53 records and create tf file to use with module https://github.com/terraform-aws-modules/terraform-aws-route53/tree/master/modules/records
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 | |
# Usage: ./export-records-terraform.sh <zone_name> | |
# Example: ./export-records-terraform.sh example.com | |
# Dependencies: jq, aws-cli | |
# This script retrieves all DNS records from AWS Route53 DNS zone | |
# and format file compatible with Terraform Module | |
# https://github.com/terraform-aws-modules/terraform-aws-route53/blob/master/examples/complete/main.tf | |
if [ -z "$1" ]; then | |
echo "Usage: ./export-records-terraform.sh <zone_name>" | |
exit 1 | |
fi | |
zone_name=$1 | |
echo ">> Retrieving DNS records from zone ${zone_name}..." | |
# Get zone slug from zone name | |
zone_slug=$(echo ${zone_name} | tr '.' '-') | |
# Get DNS zone current data from AWS | |
zone="$(aws route53 list-hosted-zones | jq '.HostedZones[] | select (.Name=="'${zone_name}'.")')" | |
zone_id=$(echo ${zone} | jq -r '.Id' | sed 's/\/hostedzone\///') | |
# Clean the file before writing new data | |
rm -f dns-zone-${zone_name}.tf | |
cat << EOF >> dns-zone-${zone_name}.tf | |
module "records" { | |
source = "terraform-aws-modules/route53/aws//modules/records" | |
version = "~> 4.0" | |
zone_name = "${zone_name}" | |
records = [ | |
EOF | |
# Retrieve all regular records (not alias) from DNS zone and write them down to terraform file | |
IFS=$'\n' | |
for dns_record in $(aws route53 list-resource-record-sets --hosted-zone-id "${zone_id}" | jq -c '.ResourceRecordSets[] | select(has("AliasTarget") | not)');do | |
name="$(echo ${dns_record} | jq -r '.Name' | sed 's/\.'${zone_name}'.//g')" | |
type="$(echo ${dns_record} | jq -r '.Type')" | |
name_slug="$(echo ${type}-${name} | sed -E 's/[\._\ ]+/-/g' | sed -E 's/(^-|-$)//g')" | |
ttl="$(echo ${dns_record} | jq -r '.TTL')" | |
records="$(echo ${dns_record} | jq -cr '.ResourceRecords' | jq '.[].Value' | sed 's/$/,/')" | |
records="$(echo ${records} | sed 's/,$//')" | |
# if type equal to SOA or NS, skip the record | |
if [ "${type}" == "SOA" ] || [ "${type}" == "NS" ]; then | |
continue | |
fi | |
cat << EOF >> dns-zone-${zone_name}.tf | |
{ | |
name = "${name}" | |
type = "${type}" | |
ttl = "${ttl}" | |
records = [${records}] | |
}, | |
EOF | |
done | |
# Retrieve all alias records from DNS zone and write them down to terraform file | |
IFS=$'\n' | |
for dns_record in $(aws route53 list-resource-record-sets --hosted-zone-id "${zone_id}" | jq -c '.ResourceRecordSets[] | select(has("AliasTarget"))');do | |
name="$(echo ${dns_record} | jq -r '.Name' | sed 's/\.'${zone_name}'.//g')" | |
type="$(echo ${dns_record} | jq -r '.Type')" | |
name_slug="$(echo ${type}-${name} | sed -E 's/[\._\ ]+/-/g' | sed -E 's/(^-|-$)//g')" | |
alias_name="$(echo ${dns_record} | jq -cr '.AliasTarget' | jq -r '.DNSName')" | |
alias_zone_id="$(echo ${dns_record} | jq -cr '.AliasTarget' | jq -r '.HostedZoneId')" | |
cat << EOF >> dns-zone-${zone_name}.tf | |
{ | |
name = "${name}" | |
type = "${type}" | |
alias = { | |
name = "${alias_name}" | |
zone_id = "${alias_zone_id}" | |
} | |
}, | |
EOF | |
done | |
cat << EOF >> dns-zone-${zone_name}.tf | |
] | |
} | |
EOF | |
echo ">> Terraform file dns-zone-${zone_name}.tf created successfully" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment