Skip to content

Instantly share code, notes, and snippets.

@leonardobiffi
Last active October 30, 2024 19:07
Show Gist options
  • Select an option

  • Save leonardobiffi/34aeab7429b8d7718857a81742ec1a03 to your computer and use it in GitHub Desktop.

Select an option

Save leonardobiffi/34aeab7429b8d7718857a81742ec1a03 to your computer and use it in GitHub Desktop.

Revisions

  1. leonardobiffi revised this gist Oct 30, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion export-records-terraform.sh
    Original file line number Diff line number Diff line change
    @@ -68,14 +68,15 @@ for dns_record in $(aws route53 list-resource-record-sets --hosted-zone-id "${zo
    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 = "${zone_id}"
    zone_id = "${alias_zone_id}"
    }
    },
    EOF
  2. leonardobiffi revised this gist Oct 30, 2024. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions export-records-terraform.sh
    Original file line number Diff line number Diff line change
    @@ -39,13 +39,18 @@ 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')"
    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}"
    @@ -59,7 +64,7 @@ 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')"
    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')"
  3. leonardobiffi revised this gist Oct 30, 2024. No changes.
  4. leonardobiffi created this gist Oct 30, 2024.
    84 changes: 84 additions & 0 deletions export-records-terraform.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #! /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')"
    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/,$//')"

    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')"
    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')"

    cat << EOF >> dns-zone-${zone_name}.tf
    {
    name = "${name}"
    type = "${type}"
    alias = {
    name = "${alias_name}"
    zone_id = "${zone_id}"
    }
    },
    EOF
    done

    cat << EOF >> dns-zone-${zone_name}.tf
    ]
    }
    EOF

    echo ">> Terraform file dns-zone-${zone_name}.tf created successfully"