diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index ad1a3fe..362cf45 100755 --- a/code/ddns-lambda.py +++ b/code/ddns-lambda.py @@ -73,7 +73,7 @@ LOGGER = logging.getLogger() account_id = None region = None -VERSION = '1.2.0b56' +VERSION = '1.2.0b57' # Read Env variables DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO') @@ -373,6 +373,10 @@ def lambda_handler( LOGGER.info("instance: %s, no instance data, repeat check: %s", instance_id, lineno()) +# instance object to/from DDB contains the instance data from get_instances (Reservations). +# We are adding a fake entry to it _DnsEntries to store what was added by the code, so we can also easily remove it later +# to avoid orphaned records + # Remove response metadata from the response if 'ResponseMetadata' in instance: instance.pop('ResponseMetadata') @@ -391,21 +395,22 @@ def lambda_handler( put_item_in_dynamodb_table(dynamodb_client, DDBNAME, instance_id, instance_attributes) LOGGER.debug("done putting item in dynamo table %s", lineno()) + dns_data = [] else: + # state is stopping or terminating # Fetch item from DynamoDB LOGGER.info("Fetching instance information from dynamodb %s", lineno()) instance = get_item_from_dynamodb_table(dynamodb_client, DDBNAME, instance_id) LOGGER.info("instance attributes: %s", str(instance) + lineno()) - -## LOGGER.info(f"Fetching instance DNS information from dynamodb {lineno()}") -# instance_dns_data = get_item_from_dynamodb_table( -# dynamodb_client, DDBNAME, f'{instance_id}/dns') -## LOGGER.info(f"got instance dns data: {instance_dns_data}") -# if instance_dns_data: -# instance_dns_data_dict = json.loads(instance_dns_data, default=json_serial) -## instance_dns_data_dict = json.loads(instance_dns_data) -# LOGGER.info( -# f"instance dns data: {str(instance_dns_data)} length {len(instance_dns_data_dict)} {lineno()}") + try: + instance_data = json.loads(instance) + dns_data_raw = instance_data.get('_DnsEntries', []) + dns_data = [dns_data_tuple(**item) for item in dns_data_raw] + LOGGER.info( + f"Found entries from DDB for DNS records: {str(dns_data)} {lineno()}") + except: + LOGGER.error(f"Cannot deserialize instance data from DDB {lineno()}") + dns_data = [] # Get the instance tags and reorder them because we want a zone created before CNAME try: @@ -732,9 +737,9 @@ def lambda_handler( " %s", str(heritage_value) + lineno()) delete_records = True - dns_data = [] # Create OR Delete the A / PTR Record if state == 'running': + dns_data = [] if not flags['noforward']: # create the records try: diff --git a/code/ddns-lambda.zip b/code/ddns-lambda.zip index 36cbb29..36c0a2b 100644 Binary files a/code/ddns-lambda.zip and b/code/ddns-lambda.zip differ