4

Update Route53 with home public IP or from EC2 Metadata

 3 years ago
source link: https://zoph.me/posts/2017-11-10-update53/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Nov 10, 2017

Update Route53 with home public IP or from EC2 Metadata

In this post, you will find how to update AWS Route53 from your current public IP (ISP) and how to use EC2 instance metadata to change your DNS A Record.

Requirements

To use this script, you will need:

  • awscli (pip install awscli)
  • dig (apt-get install dnsutils)

Update Route53 with your current home Public IP

First, I was facing an issue with my current ISP with non-static IP address, so I found a useful script to update AWS Route53 daily from my RaspberryPi.

Please find the how-to on my GitHub

Update Route53 with your current EC2 Metadata from an instance in an ASG

I didn’t want to setup an ALB/ELB because it cost too much for my needs (labs). So I’ve adapted this article to meet my needs.

Follow the same steps and adapt Update53-EC2Pub.sh and UserData from my Launch Configuration below:

update-route53-A.json & Update53-EC2Pub.sh

{
  "Comment": "Update the A record set",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "MY_DNS_RECORD_NAME",
        "Type": "A",
        "TTL": 60,
        "ResourceRecords": [
          {
            "Value": "127.0.0.1"
          }
        ]
      }
    }
  ]
}
#!/bin/sh

if [ -z "$1" ]; then 
    echo "IP not given...trying EC2 metadata...";
    IP=$( curl -s http://169.254.169.254/latest/meta-data/public-ipv4 )  
else 
    IP="$1" 
fi 
echo "IP to update: $IP"

HOSTED_ZONE_ID=$( aws route53 list-hosted-zones-by-name | grep -B 1 -e "YOUR_PARENT_DOMAIN_NAME" | sed 's/.*hostedzone\/\([A-Za-z0-9]*\)\".*/\1/' | head -n 1 )
echo "Hosted zone being modified: $HOSTED_ZONE_ID"

INPUT_JSON=$( cat /home/ec2-user/update53/update-route53-A.json | sed "s/127\.0\.0\.1/$IP/" )

# http://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html
# We want to use the string variable command so put the file contents (batch-changes file) in the following JSON
INPUT_JSON="{ \"ChangeBatch\": $INPUT_JSON }"

aws route53 change-resource-record-sets --hosted-zone-id "$HOSTED_ZONE_ID" --cli-input-json "$INPUT_JSON"

UserData

In this example, I’ve compressed and hosted source in a simple S3 bucket, and using it with UserData instance.

#!/bin/bash
/usr/bin/aws s3 cp  s3://YOU_S3_BUCKET/update53.tar.bz2  /home/ec2-user/
tar xjvf /home/ec2-user/update53.tar.bz2 -C /home/ec2-user/
rm /home/ec2-user/update53.tar.bz2
/bin/sh /home/ec2-user/update53/update.sh >> /home/ec2-user/update53.log
rm -rf /home/ec2-user/update53/

It’s working pretty well, with scheduled actions on my ASG, my instance is shutting down at night every day, and fire-up on the next morning, the public IP is automatically updated to my AWS Route53 A Record.

Don’t hesitate to ping me with any question

That’s all folks!

zoph.


bashcodingawsdnsraspberrypiroute53dyndns

353 Words

2017-11-10 20:31 +0000


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK