diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index 979b44a..8e7827c 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.0b65' +VERSION = '1.2.0b66' # Read Env variables DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO') @@ -695,19 +695,21 @@ def lambda_handler( return caller_response if tag_data['option_cname'].valid: - LOGGER.info( - f"4.1 instance: {instance_id}, CNAME using tag_option.cname hostname {tag_data['option_cname'].hostname} and tag_option.cname {tag_data['option_cname'].zonename}") - cf_hostname = tag_data['option_cname'].hostname - cf_zonename = tag_data['option_cname'].zonename - elif not tag_data['option_nname'].valid and tag_data['option_name'].hostname and f_zonename: - LOGGER.info(f"4.2 instance: {instance_id}, CNAME using tag_option.cname hostname {tag_data['option_cname'].hostname} and current zone {f_zonename}") - cf_hostname = tag_data['option_cname'].hostname - cf_zonename = f_zonename - else: - LOGGER.info(f"4.3 instance: {instance_id}, CNAME no valid hostname or zone found, skipping CNAME") - cf_hostname = None - cf_zonename = None - cf_fqdn=create_fqdn(cf_hostname, cf_zonename) + LOGGER.info( + f"4.1 instance: {instance_id}, CNAME using tag_option.cname hostname {tag_data['option_cname'].hostname} and tag_option.cname {tag_data['option_cname'].zonename}") + cf_hostname = tag_data['option_cname'].hostname + cf_zonename = tag_data['option_cname'].zonename + elif not tag_data['option_cname'].valid and tag_data['option_name'].hostname and f_zonename: + LOGGER.info( + f"4.2 instance: {instance_id}, CNAME using tag_option.cname hostname {tag_data['option_cname'].hostname} and current zone {f_zonename}") + cf_hostname = tag_data['option_cname'].hostname + cf_zonename = f_zonename + else: + LOGGER.info( + f"4.3 instance: {instance_id}, CNAME no valid hostname or zone found, skipping CNAME") + cf_hostname = None + cf_zonename = None + cf_fqdn = create_fqdn(cf_hostname, cf_zonename) # note this will not continue and set a cname @@ -902,17 +904,17 @@ def lambda_handler( # so using existing tag data will not be valid if state != 'running': - heritage_records = {} - for entry in dns_data: - if entry.rr_type == 'TXT' and "heritage=" in entry.rr_value: - heritage_records[entry.rr_name] = entry.rr_value - for entry in dns_data: - if not (entry.rr_type == 'TXT' and "heritage=" in entry.rr_value): - process_response = new_process_delete_records( - instance_id, entry.zone_id, entry.rr_name, entry.zone_name, entry.rr_type, entry.rr_value, heritage_records.get(entry.rr_name, '')) - delete_records = delete_records and process_response['delete_success'] - caller_response = caller_response + process_response['msg'] - count[f"delete_success.{process_response.get('delete_success')}"] += 1 + heritage_records = {} + for entry in dns_data: + if entry.rr_type == 'TXT' and "heritage=" in entry.rr_value: + heritage_records[entry.rr_name] = entry.rr_value + for entry in dns_data: + if not (entry.rr_type == 'TXT' and "heritage=" in entry.rr_value): + process_response = new_process_delete_records( + instance_id, entry.zone_id, entry.rr_name, entry.zone_name, entry.rr_type, entry.rr_value, heritage_records.get(entry.rr_name, '')) + delete_records = delete_records and process_response['delete_success'] + caller_response = caller_response + process_response['msg'] + count[f"delete_success.{process_response.get('delete_success')}"] += 1 # # Process and delete A record and associated TXT record # process_response = process_delete_records( @@ -925,7 +927,7 @@ def lambda_handler( # private_ip, # heritage_value # ) -# +# # # only true if existing delete_records and the delete_success from the subroutine is true # delete_records = delete_records and process_response['delete_success'] # # append to the lsit @@ -953,7 +955,8 @@ def lambda_handler( # Process the CNAME record only if it has passed the check # if tag_data['option_cname'].valid: if cf_hostname: - LOGGER.debug(f"cname record is valid - creating CNAME record host {cf_hostname} zone {cf_zonename}: {lineno()}") + LOGGER.debug( + f"cname record is valid - creating CNAME record host {cf_hostname} zone {cf_zonename}: {lineno()}") # cname_host_name = tag_data['option_cname'].hostname # cname_domain_suffix = tag_data['option_cname'].zonename # cname_domain_suffix_item = phz_collection_by_vpc[cname_domain_suffix] @@ -966,19 +969,19 @@ def lambda_handler( # create CNAME record in private zone if state == 'running': try: -# LOGGER.debug("cname_host_name:" -# " %s", str(cname_host_name) + lineno()) -# LOGGER.debug("cname_domain_suffix:" -# " %s", str(cname_domain_suffix) + lineno()) -# LOGGER.debug("cname_domain_suffix_id:" -# " %s", str(cname_domain_suffix_id) + lineno()) -# + # LOGGER.debug("cname_host_name:" + # " %s", str(cname_host_name) + lineno()) + # LOGGER.debug("cname_domain_suffix:" + # " %s", str(cname_domain_suffix) + lineno()) + # LOGGER.debug("cname_domain_suffix_id:" + # " %s", str(cname_domain_suffix_id) + lineno()) + # create_response = create_resource_record( route53, instance_id, -# cname_domain_suffix_id, -# cname_host_name, -# cname_domain_suffix, + # cname_domain_suffix_id, + # cname_host_name, + # cname_domain_suffix, cf_zonename_id, cf_hostname, cf_zonename, @@ -991,7 +994,8 @@ def lambda_handler( if create_response == 'success': dns_data.append(dns_data_tuple( cf_zonename_id, cf_hostname, cf_zonename, 'CNAME', cf_fqdn)) - LOGGER.info(f"instance: {instance_id}, Created {append_msg} {lineno()}") + LOGGER.info( + f"instance: {instance_id}, Created {append_msg} {lineno()}") caller_response.append('Created ' + append_msg) else: caller_response.append(create_response) @@ -1004,14 +1008,15 @@ def lambda_handler( try: if not flags['noheritage']: if len(heritage) > 0: - cf_hostname_txt=TXT_RR_PREFIX+'.'+cf_hostname - LOGGER.debug(f"Creating heritage TXT resource records host {cf_hostname_txt} zone {cf_zonename} value {heritage_value} {lineno()}") + cf_hostname_txt = TXT_RR_PREFIX + '.' + cf_hostname + LOGGER.debug( + f"Creating heritage TXT resource records host {cf_hostname_txt} zone {cf_zonename} value {heritage_value} {lineno()}") create_response = create_resource_record( route53, instance_id, -# cname_domain_suffix_id, -# cname_host_name_txt, -# cname_domain_suffix, + # cname_domain_suffix_id, + # cname_host_name_txt, + # cname_domain_suffix, cf_zonename_id, cf_hostname_txt, cf_zonename, @@ -1024,15 +1029,18 @@ def lambda_handler( if create_response == 'success': dns_data.append(dns_data_tuple( cf_zonename_id, cf_hostname_txt, cf_zonename, 'TXT', heritage_value)) - LOGGER.info(f"instance: {instance_id}, Created {append_msg} {lineno()}") + LOGGER.info( + f"instance: {instance_id}, Created {append_msg} {lineno()}") caller_response.append('Created ' + append_msg) else: caller_response.append(create_response) caller_response.append('Failed to create ' + append_msg) - LOGGER.error(f"Failed to create TXT for CNAME record: {create_response}") + LOGGER.error( + f"Failed to create TXT for CNAME record: {create_response}") except BaseException as err: - LOGGER.error(f"instance: {instance_id}, unexpected error: {err} {lineno()}") + LOGGER.error( + f"instance: {instance_id}, unexpected error: {err} {lineno()}") # # not running, so process delete CNAME and associated TXT record # else: @@ -1047,12 +1055,12 @@ def lambda_handler( # final_private_dns_name, # heritage_value # ) -# +# # # only true if existing delete_records and the delete_success from the subroutine is true # delete_records = delete_records and process_response['delete_success'] # # append to the lsit # caller_response = caller_response + process_response['msg'] -# +# # update ddb entry to include dns entries written to be able to delete them properly if state == 'running': try: diff --git a/code/ddns-lambda.zip b/code/ddns-lambda.zip index c342468..099955a 100644 Binary files a/code/ddns-lambda.zip and b/code/ddns-lambda.zip differ