Skip to content

Instantly share code, notes, and snippets.

@jkordish
Last active January 16, 2019 10:56
Show Gist options
  • Save jkordish/86883edab2156d5917bc to your computer and use it in GitHub Desktop.
Save jkordish/86883edab2156d5917bc to your computer and use it in GitHub Desktop.
AWS EMR VPC DNS fix when using your own Internal DNS servers
#!/bin/bash -e
###
#
# Using custom DNS in your AWS VPC and tired of it breaking EMR?
# Assumes you have a internal DNS server at the network address at .5 and DNS01
# Just an example -- hack away to fit your specific needs.
# Add this to fix to a S3 bucket and then add it your first boot strap step either via the console or cli
# e.g.
# --bootstrap-actions Path=s3://<bucket>/emr_fix_dns.sh Path=s3://support.elasticmapreduce/spark/install-spark
#
###
s#!/bin/bash -ex
set +o pipefail
sudo tee /usr/local/bin/nsupdate <<NSUPDATE
#!/bin/bash -ex
IPV4=\`/opt/aws/bin/ec2-metadata -o | cut -d' ' -f2\`
NETWORK=\`/opt/aws/bin/ec2-metadata -o | cut -d' ' -f2 | cut -d. -f1-3\`
DOMAIN=\`/usr/bin/dig @\${NETWORK}.5 -x \${NETWORK}.5 +short | cut -d. -f2- | head -1\`
HOSTNAME=\`/bin/hostname -s\`
ZONE=\`/usr/bin/dig @\${NETWORK}.5 -x \${NETWORK}.5 +short | cut -d. -f2- | head -1\`
REVERSE_LOOKUP_ZONE=\`/usr/bin/dig -x \${IPV4} |grep -i -A1 question | grep -m 1 arpa| cut -f1 | cut -c5- | sed -e 's/^\.//' | tr '[:lower:]' '[:upper:]'\`
ARPA_NAME=\`/usr/bin/dig -x \${IPV4} |grep -i -A1 question | grep -m 1 arpa| cut -f1 | cut -c2- | tr '[:lower:]' '[:upper:]'\`
SERVER=\`/usr/bin/dig @\${NETWORK}.5 -x \${NETWORK}.5 +short | cut -d. -f2 | head -1\`
cat<<EOF | /usr/bin/nsupdate -v
server \${SERVER}-dns01.\${ZONE}
zone \${ZONE}
update delete \${HOSTNAME}.\${ZONE}
update add \${HOSTNAME}.\${ZONE} 3600 A \${IPV4}
show
send
zone \${REVERSE_LOOKUP_ZONE}
update add \${ARPA_NAME} 3600 IN PTR \${HOSTNAME}.\${ZONE}
show
send
EOF
NSUPDATE
sudo /bin/chmod +x /usr/local/bin/nsupdate
echo "/usr/local/bin/nsupdate" | sudo tee -a /etc/rc.local
sudo /usr/local/bin/nsupdate | true
#!/bin/bash -ex
set +o pipefail
sudo tee /usr/local/bin/nsupdate <<NSUPDATE
#!/bin/bash -ex
IPV4=\`/opt/aws/bin/ec2-metadata -o | cut -d' ' -f2\`
NETWORK=\`/opt/aws/bin/ec2-metadata -o | cut -d' ' -f2 | cut -d. -f1-3\`
DOMAIN=\`/usr/bin/dig @\${NETWORK}.5 -x \${NETWORK}.5 +short | cut -d. -f2- | head -1\`
HOSTNAME=\`/bin/hostname -s\`
ZONE=\`/usr/bin/dig @\${NETWORK}.5 -x \${NETWORK}.5 +short | cut -d. -f2- | head -1\`
REVERSE_LOOKUP_ZONE=\`/usr/bin/dig -x \${IPV4} |grep -i -A1 question | grep -m 1 arpa| cut -f1 | cut -c5- | sed -e 's/^\.//' | tr '[:lower:]' '[:upper:]'\`
ARPA_NAME=\`/usr/bin/dig -x \${IPV4} |grep -i -A1 question | grep -m 1 arpa| cut -f1 | cut -c2- | tr '[:lower:]' '[:upper:]'\`
SERVER=\`/usr/bin/dig @\${NETWORK}.5 -x \${NETWORK}.5 +short | cut -d. -f2 | head -1\`
cat<<EOF | /usr/bin/nsupdate -v
server \${SERVER}-proxy01.\${ZONE}
zone \${ZONE}
update delete \${HOSTNAME}.\${ZONE}
update add \${HOSTNAME}.\${ZONE} 3600 A \${IPV4}
show
send
zone \${REVERSE_LOOKUP_ZONE}
update add \${ARPA_NAME} 3600 IN PTR \${HOSTNAME}.\${ZONE}
show
send
EOF
NSUPDATE
sudo /bin/chmod +x /usr/local/bin/nsupdate
echo "/usr/local/bin/nsupdate" | sudo tee -a /etc/rc.local
sudo /usr/local/bin/nsupdate | true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment