Created
December 14, 2017 11:04
-
-
Save alexpdp7/dc965b6e68880dc8acfc1df2f25814c2 to your computer and use it in GitHub Desktop.
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
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