Skip to content

Instantly share code, notes, and snippets.

@foxutech
Created August 19, 2018 17:58
Show Gist options
  • Save foxutech/832f7c7797da31b51a701045f503219e to your computer and use it in GitHub Desktop.
Save foxutech/832f7c7797da31b51a701045f503219e to your computer and use it in GitHub Desktop.
Auto-register an EC2 host with Route53 DNS #route53 #aws #ec2 https://foxutech.com
#!/bin/sh
# Script to bind a CNAME to our HOST_NAME in ZONE
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Defaults
TTL=60
HOST_NAME=`hostname`
ZONE=NoZoneDefined
# Load configuration
. /etc/route53/config
# Export access key ID and secret for cli53
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
# Use command line scripts to get instance ID and public hostname
#INSTANCE_ID=$(ec2metadata | grep 'instance-id:' | cut -d ' ' -f 2)
MY_NAME=$HOST_NAME
PUBLIC_HOSTNAME=$(/opt/aws/bin/ec2-metadata | grep 'public-hostname:' | cut -d ' ' -f 2)
logger "ROUTE53: Setting DNS CNAME $MY_NAME.$ZONE to $PUBLIC_HOSTNAME"
# Create a new CNAME record on Route 53, replacing the old entry if nessesary
/usr/bin/cli53 rrcreate "$ZONE" "$MY_NAME" CNAME "$PUBLIC_HOSTNAME" --replace --ttl "$TTL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment