Skip to content

Instantly share code, notes, and snippets.

@alexpdp7
Created December 14, 2017 11:04
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 alexpdp7/dc965b6e68880dc8acfc1df2f25814c2 to your computer and use it in GitHub Desktop.
Save alexpdp7/dc965b6e68880dc8acfc1df2f25814c2 to your computer and use it in GitHub Desktop.
import json
import boto3
# To use, connect to an API gateway, grant route53 permissions and:
#
# curl "https://your_api_gateway_host/prod/updateDdns?host_name=host_name_to_update&secret=matching_secret"
#
# or
#
# curl "https://your_api_gateway_host/prod/updateDdns?host_name=host_name_to_update&secret=matching_secret&ip=override_autodetected_ip"
HOST_NAME_SECRETS = {
'ddns-host.example.com': '3278461871',
}
HOSTED_ZONE_ID = '123456' # id of your hosted zone
def lambda_handler(event, context):
ip = event['queryStringParameters'].get('ip',
event['headers']['X-Forwarded-For'].split(',')[0])
host_name = event['queryStringParameters']['host_name']
secret = event['queryStringParameters']['secret']
if HOST_NAME_SECRETS[host_name] != secret:
raise Exception('Bad secret')
route53 = boto3.client('route53')
route53.change_resource_record_sets(
HostedZoneId=HOSTED_ZONE_ID,
ChangeBatch={
'Comment': 'lambda ddns update',
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': host_name,
'Type': 'A',
'TTL': 60,
'ResourceRecords': [
{
'Value': ip
},
],
}
}
]
}
)
return {
'statusCode': 200,
'headers': { 'Content-Type': 'application/json' },
'body': json.dumps({
'ip': ip,
'host_name': host_name,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment