diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index 4804a9e..8d10ab5 100755 --- a/code/ddns-lambda.py +++ b/code/ddns-lambda.py @@ -73,7 +73,7 @@ LOGGER = logging.getLogger() account_id = None region = None -VERSION = '2.0.1' +VERSION = '2.0.2' # Read Env variables DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO') @@ -800,10 +800,19 @@ def lambda_handler( f"instance: {instance_id}, final names for A and PTR record host {f_hostname} zone {f_zonename} fqdn {final_private_dns_name} ptr-fqdn {p_fqdn} {lineno()}") # Get the PHZ ID for the Zone - zone_data_forward = zone_data_tuple( - *phz_collection_by_vpc[final_hosted_zone_name].values()) - zone_data_reverse = zone_data_tuple( - *phz_collection_by_vpc[tag_data['ptr_entry'].zonename].values()) + zone_forward_item = phz_collection_by_vpc.get(final_hosted_zone_name, {}) + if zone_forward_item: + zone_data_forward = zone_data_tuple( + *phz_collection_by_vpc[final_hosted_zone_name].values()) + else: + zone_data_forward = zone_data_tuple(None, None, None, None, None) + + zone_reverse_item = phz_collection_by_vpc.get(tag_data['ptr_entry'].zonename], {}) + if zone_reverse_item: + zone_data_reverse = zone_data_tuple(*phz_collection_by_vpc[tag_data['ptr_entry'].zonename].values()) + else: + zone_data_reverse = zone_data_tuple(None, None, None, None, None) + # final_hosted_zone_item = phz_collection_by_vpc[final_hosted_zone_name] # final_hosted_zone_id = zone_data_forward.zone_id # final_hosted_zone_owner = final_hosted_zone_item['owner_account'] @@ -815,7 +824,7 @@ def lambda_handler( f"private_hosted_zone: zone_id {zone_data_forward.zone_id} is_mine {zone_data_forward.owner_account==account_id} owner {zone_data_forward.owner_account}: {lineno()}") # create the TXT heritage record - heritage = initialize_heritage(HERITAGE_TAG, VERSION, + heritage=initialize_heritage(HERITAGE_TAG, VERSION, { 'account_id': account_id, 'region': region, @@ -834,7 +843,7 @@ def lambda_handler( # Create OR Delete the A / PTR Record if state == 'running': dns_data = [] - if not flags['noforward']: + if not flags['noforward'] and zone_data_forward.zone_id: # create the records try: LOGGER.debug("Creating resource records %s", lineno()) @@ -901,10 +910,14 @@ def lambda_handler( LOGGER.error("instance: %s, unexpected error. %s\n", instance_id, str(err) + lineno()) else: + if not zone_data_forward.zone_id: + LOGGER.info( + f"not adding A and heritage TXT for host {final_private_hostname} zone {zone_data_forward.name} no such zone value {private_ip}") + else: LOGGER.info( f"flags=noforward, not adding A and heritage TXT for host {final_private_hostname} zone {zone_data_forward.name} value {private_ip}") - if not flags['noptr']: + if not flags['notpr'] and zone_data_reverse.zone_id: # fqdn = create_fqdn(final_private_hostname, final_hosted_zone_name) try: if reverse_zone_associated: @@ -972,7 +985,11 @@ def lambda_handler( LOGGER.error("instance: %s, unexpected error. %s\n", instance_id, str(err) + lineno()) else: - LOGGER.info( + if not zone_data_reverse.zone_id: + LOGGER.info( + f"not adding PTR and heritage TXT for host {tag_data['ptr_entry'].hostname} zone {tag_data['ptr_entry'].zonename} no such zone value {final_private_dns_name}") + else: + LOGGER.info( f"flags=noptr, not adding PTR and heritage TXT for host {tag_data['ptr_entry'].hostname} zone {tag_data['ptr_entry'].zonename} value {final_private_dns_name}") # else: # not running so delete the records. Note this may leave orphans around if the flags are set and then the host is shut down. We may want to remove no matter what. @@ -1009,12 +1026,16 @@ def lambda_handler( # cname_domain_suffix_id = cname_domain_suffix_item['zone_id'] # LOGGER.debug("cname_domain_suffix_id: %s", str(cname_domain_suffix_id)) - cf_zonename_id = phz_collection_by_vpc[cf_zonename]['zone_id'] + cf_zonename_item = phz_collection_by_vpc.get(cf_zonename,None) + if cf_zonename_item: + cf_zonename_id = cf_zonename_item['zone_id'] + else: + cf_zonename_id = None LOGGER.debug(f"cname_domain_suffix_id: {cf_zonename_id}") # create CNAME record in private zone if state == 'running': - if not flags['nocname'] and (all([emr_status.is_cluster, emr_status.is_master]) or not emr_status.is_cluster): + if not flags['nocname'] cf_zonename_id and (all([emr_status.is_cluster, emr_status.is_master]) or not emr_status.is_cluster): try: LOGGER.debug(f"cname_host_name: {cf_hostname} {lineno()}") LOGGER.debug(f"cname_domain_suffix: {cf_zonename} {lineno()}") @@ -1093,6 +1114,9 @@ def lambda_handler( if emr_status.is_cluster: LOGGER.info( f"instance {instance_id}: is_cluster && not is_master cluster_id {emr_status.cluster_id} NOT setting CNAME {cf_hostname} in zone {cf_zonename} {lineno()}") + elif not cf_zonename_id: + LOGGER.info( + f"instance {instance_id}: NOT setting CNAME {cf_hostname} in zone {cf_zonename} no such zone {lineno()}") # # update ddb entry to include dns entries written to be able to delete them properly diff --git a/code/ddns-lambda.zip b/code/ddns-lambda.zip index 80f8206..40c89d4 100644 Binary files a/code/ddns-lambda.zip and b/code/ddns-lambda.zip differ