-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple script to update AWS Route 53 from a dynamic ip host
I run this in a cron job to update my home VPN server
- Loading branch information
0 parents
commit 97797de
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import boto3 | ||
import requests | ||
import re | ||
import time | ||
|
||
# Replace below with correct hosted zone id, host name, and desired TTL | ||
hostedZoneId = 'ZK8XMWWB*****' | ||
hostName = 'host.dyn.example.com' | ||
ttl = 60 | ||
|
||
# Any ip lookup service that returns just the ip address as text | ||
ipifyEndpoint = 'https://api.ipify.org' | ||
|
||
validIpRegex = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' | ||
|
||
thisPublicIp = requests.get(ipifyEndpoint).text | ||
|
||
validIpPattern = re.compile(validIpRegex) | ||
|
||
validIpMatch = validIpPattern.match(thisPublicIp) | ||
|
||
if validIpMatch == None: | ||
# Not a valid ip address | ||
exit('Invalid IP Address: ' + thisPulbicIp) | ||
|
||
route53 = boto3.client('route53') | ||
|
||
route53.change_resource_record_sets( | ||
HostedZoneId = hostedZoneId, | ||
ChangeBatch = { | ||
'Comment' : 'Last updated by route53me.py at ' % time.gmtime(), | ||
'Changes' : [ | ||
{ | ||
'Action' : 'UPSERT', | ||
'ResourceRecordSet' : | ||
{ | ||
'Name' : hostName, | ||
'Type' : 'A', | ||
'TTL' : ttl, | ||
'ResourceRecords' : | ||
[ | ||
{ | ||
'Value' : thisPublicIp | ||
}, | ||
] | ||
} | ||
}, | ||
] | ||
} | ||
) | ||
|
||
print ('Updated to ' + thisPublicIp) | ||
|
||
|