Created
December 29, 2020 22:27
-
-
Save tspycher/a0b262f91b3aef7527e335e555e83257 to your computer and use it in GitHub Desktop.
Update Route53 Entry with IP from dyndns clients
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 | |
import base64 | |
import os | |
def lambda_handler(event, context): | |
client = boto3.client('route53') | |
username,password = base64.b64decode(event.get("headers").get("Authorization").replace("Basic ", "")).decode().split(":") | |
if not password or not password == os.environ.get("password"): | |
return { | |
'statusCode': 403, | |
'body': "Your Password is wrong" | |
} | |
parameters = event.get("queryStringParameters", {}) | |
hostname = parameters.get('hostname') | |
ip = parameters.get('myip') | |
source_ip = event.get('requestContext').get('identity').get('sourceIp') | |
if source_ip != ip: | |
print("queryStringParameters IP {} is not equal sourceIP {}".format(ip, source_ip)) | |
return { | |
'statusCode': 400, | |
'body': "queryStringParameters IP {} is not equal sourceIP {}".format(ip, source_ip) | |
} | |
response = client.change_resource_record_sets( | |
HostedZoneId=username, | |
ChangeBatch={ | |
"Comment": "Automatic DNS update", | |
"Changes": [ | |
{ | |
'Action': 'UPSERT', # INSERT, really! | |
'ResourceRecordSet' : { | |
"Name": hostname, | |
'Type' : 'A', | |
#"Region": "eu-west-1", | |
'TTL' : 15, | |
'ResourceRecords' : [ | |
{'Value': ip} | |
] | |
} | |
} | |
] | |
} | |
) | |
return { | |
'statusCode': 200, | |
'body': "DNS updated" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment