Skip to content

Instantly share code, notes, and snippets.

@jaidhyani
Last active July 15, 2017 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaidhyani/75074caaa34e57e42e1600a708e5f49d to your computer and use it in GitHub Desktop.
Save jaidhyani/75074caaa34e57e42e1600a708e5f49d to your computer and use it in GitHub Desktop.
# this is a script to dynamically update a route53 dns record to point to the
# IP address that the machine that is running on
# before running it, you'll want to create an IAM user with the following policy
# attched, replacing YOURZONEID with the zone ID of the domain you're going to be
# updating in route53.
# IAM policy:
"""
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1499724866000",
"Effect": "Allow",
"Action": [
"route53:*"
],
"Resource": [
"arn:aws:route53:::hostedzone/YOURZONEID"
]
},
{
"Sid": "Stmt1499724946000",
"Effect": "Allow",
"Action": [
"route53:*"
],
"Resource": [
"arn:aws:route53:::change/YOURZONEID"
]
}
]
}
"""
# You'll then want to save the credentials
# of this IAM user in $HOME/.aws/credentials, or wherever else boto looks
# for credentials on your system, like this:
"""
[dyn53]
aws_access_key_id = ABABBABABABBBAB
aws_secret_access_key = 89dsuiw34enr4we3098rewfjews0ijesd
"""
import boto3
import requests
import time
zid=u'' #get your zone ID from route 53 dashboard, looks like Z123QWERTYUI45
hostname = u'' #what dns name you're trying to point at your machine, e.g. myhouse.myname.net
def get_ip():
while True:
req = requests.get('http://jsonip.com')
try:
if req.ok:
return req.json()['ip']
except Exception:
pass
def get_record_ip(c):
rec_resp = c.list_resource_record_sets(HostedZoneId=zid)
rrs = rec_resp['ResourceRecordSets']
try:
rec = (r for r in rrs if r['Name'] == hostname).__next__()
ip = rec['ResourceRecords'][0]['Value']
return ip
except Exception:
return ''
def update_ip(c, ip):
resp = c.change_resource_record_sets(
HostedZoneId=zid,
ChangeBatch={
'Changes': [{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': hostname,
'Type': 'A',
'TTL': 300,
'ResourceRecords': [{'Value': ip}],
}
}]
}
)
return resp
session = boto3.Session(profile_name='dyn53')
client = session.client('route53')
while True:
curr_ip = get_ip()
rec_ip = get_record_ip(client)
needs_update = curr_ip != rec_ip
if needs_update:
print('updating IP')
update_ip(client, curr_ip)
time.sleep(30)
@jaidhyani
Copy link
Author

This is an EXTREMELY HACKY script to dynamically update a route53 DNS record to point at the public IP address of the machine running the script. This is useful if you're hosting something on your home machine and want to access it from elsewhere, but your ISP keeps changing your IP address. In my case, I've been playing around with CUDA and deep learning on my GPU, and I wanted to be able to work from the coffee shop up the street.

Again, this is very hacky and could stand to be improved (this has silenced errors, and I think the permissions on the IAM profile are slightly too broad - if you can, you want o restrict it to updating just the domain name in question, right now it has access to updating the whole zone.

Don't forget that you'll need to set up port forwarding or a DMZ on your router to access your desktop/other machine in the house/office/secret hacker den.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment