diff --git a/README.md b/README.md index 031bf74..528073b 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ No modules. | [enable\_sns](#input\_enable\_sns) | Enable use of SNS for reporting errors | `bool` | `false` | no | | [enable\_sqs](#input\_enable\_sqs) | Enable use of SQS for SNS to send errors. Requires the use of enable\_sns as well | `bool` | `false` | no | | [kms\_key\_name](#input\_kms\_key\_name) | Different KMS Key (for SNS and SQS) to override default of var.name | `string` | `null` | no | -| [lambda\_environment\_variables](#input\_lambda\_environment\_variables) | Map of lambda environment variables and values | `map(string)` |
{
"DNS_RR_TimeToLive": 60,
"DebugLogLevel": "INFO",
"DynamoDBName": null,
"HeritageIdentifier": "dynr53",
"HeritageTXTRecordPrefix": "_txt",
"MaxApiRetry": 10,
"SleepTime": 60,
"SnsEnable": false,
"SnsTopicArn": "",
"TagKeyCname": "boc:dns:cname",
"TagKeyHostName": "boc:dns:name",
"TagKeyZone": "boc:dns:zone"
}
| no | +| [lambda\_environment\_variables](#input\_lambda\_environment\_variables) | Map of lambda environment variables and values | `map(string)` |
{
"DNS_RR_TimeToLive": 60,
"DebugLogLevel": "INFO",
"DynamoDBName": null,
"HeritageIdentifier": "dynr53",
"HeritageTXTRecordPrefix": "_txt",
"MaxApiRetry": 10,
"SleepTime": 60,
"SnsEnable": false,
"SnsTopicArn": "",
"TagKeyCname": "boc:dns:cname",
"TagKeyFlags": "boc:dns:flags",
"TagKeyHostName": "boc:dns:name",
"TagKeyPtrname": "boc:dns:ptrname",
"TagKeyZone": "boc:dns:zone"
}
| no | | [lambda\_environment\_variables\_override](#input\_lambda\_environment\_variables\_override) | Map of lambda environment variables and values to override from the defaults | `map(string)` | `{}` | no | | [lambda\_name](#input\_lambda\_name) | Different Lambda name to override default of var.name | `string` | `null` | no | | [name](#input\_name) | Name to use within all the created resources (default: inf-dynamic-route53) | `string` | `"inf-dynamic-route53"` | no | diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index 6c1bac8..c4f40ff 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.0b85' +VERSION = '1.2.0b86' # Read Env variables DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO') @@ -667,6 +667,9 @@ def lambda_handler( tag_data['name'] = tag_data_tuple(*process_tags_name(tags)) LOGGER.debug("New tag_data structure: %s", str(pformat(tag_data)) + lineno()) + emr_status = discover_emr(tags_dict) + LOGGER.info(f"discover_emr instance: {instance_id} result {emr_status}") + default_hostname = '-'.join(['ip'] + private_ip.split('.')) if tag_data['option_zone'].valid: if tag_data['option_name'].valid: @@ -877,7 +880,7 @@ def lambda_handler( 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['noreverse']: + if not flags['noptr']: # fqdn = create_fqdn(final_private_hostname, final_hosted_zone_name) try: if reverse_zone_associated: @@ -946,7 +949,7 @@ def lambda_handler( instance_id, str(err) + lineno()) else: LOGGER.info( - f"flags=noreverse, not adding PTR and heritage TXT for host {tag_data['ptr_entry'].hostname} zone {tag_data['ptr_entry'].zonename} value {final_private_dns_name}") + 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. # go through the dns_data records, and delete them. dns_data contains the records that were added. It is possible the tags have changed @@ -1025,11 +1028,14 @@ def lambda_handler( # create CNAME record in private zone if state == 'running': try: - if not flags['nocname']: - LOGGER.debug(f"cname_host_name: {cf_hostname} {lineno()}") - LOGGER.debug(f"cname_domain_suffix: {cf_zonename} {lineno()}") - LOGGER.debug(f"cname_domain_suffix_id: {cf_zonename_id} {lineno()}") - LOGGER.debug(f"cname_target: {final_private_dns_name} {lineno()}") + if not flags['nocname'] and (all(emr_status.is_cluster, emr_status.is_master) or not emr_status.is_cluster) + LOGGER.debug(f"cname_host_name: {cf_hostname} {lineno()}") + LOGGER.debug(f"cname_domain_suffix: {cf_zonename} {lineno()}") + LOGGER.debug(f"cname_domain_suffix_id: {cf_zonename_id} {lineno()}") + LOGGER.debug(f"cname_target: {final_private_dns_name} {lineno()}") + if emr_status.is_cluster: + LOGGER.info( + f"instance {instance_id}: is_cluster && is_master cluster_id {cluster_id} setting CNAME {cf_hostname} in zone {cf_zonename} {lineno()}") create_response = create_resource_record( route53, @@ -3096,7 +3102,12 @@ def tags_to_dict(tags): def process_tags_flags(tags): """ - Process the DNS flags tags into for tags[key]=='boc:dns:flags' + Process the DNS flags tags into for tags[key]=='boc:dns:flags'. Available flags: + + - noforward: do not define A or AAAA (when available) or the associated heritage TXT record + - noptr: do not define a PTR or associated heritage TXT record with default or boc:dns:ptrname flag + - nocname: do not define a CNAME or associated heritage TXT record, even if specified in the boc:dns:cname flag + - noheritage: do not create a heritage TXT record, used to indicate which service created the entries :param list(dict(string)) tags: tags from instance, list of dict of string. Keys and values turned to lowercase. :return dict(string): flag settings in defaultdict for controlling which names are registered and when @@ -3269,10 +3280,6 @@ def parse_hostname_to_components(name): return (False, host, domain) -# noforward -# noreverse -# noheritage - def create_fqdn(host, zone): """ This takes a hostname (may or nay not be FQDN) and a zone, and returns the proper concatenation of the two, with a trailing dot. @@ -3287,7 +3294,7 @@ def create_fqdn(host, zone): return fqdn -def discover_emr_master(tags): +def discover_emr_cluster(tags): """ This tags a dict of tags and determines if the appropriate EMR tags are set. @@ -3298,16 +3305,17 @@ def discover_emr_master(tags): - emr:elasticmapreduce:instance-group-role == MASTER :param dict tags: dict of tag - :return (bool,str): Tuple containing is_master (is an EMR node and is the master), and the cluster_id if it's a cluster. cluster_id will be empty if not a cluster. + :return (bool,str): Tuple containing is_master, is_cluster, and cluster_id if it's a cluster. cluster_id will be empty if not a cluster. """ + cluster_tuple = namedtuple('EMRCluster', ['cluster', 'master', 'cluster_id']) cluster_id = tags.get('aws:elasticmapreduce:job-flow-id', '') is_master = tags.get('aws:elasticmapreduce:instance-group-role', '') == 'MASTER' - is_cluster = cluster_id != '' - LOGGER.debug( - f"discover_emr: is_cluster {is_cluster}, is_master {is_master}, cluster_id {cluster_id}") - return (is_master and is_cluster, cluster_id) + + result = cluster_tuple(is_cluster, is_master, cluster_id) + LOGGER.debug(f"discover_emr: {str(result)} {lineno()}") + return result def evaluate_event_action(event): diff --git a/code/ddns-lambda.zip b/code/ddns-lambda.zip index 1fd45a6..3d5c84a 100644 Binary files a/code/ddns-lambda.zip and b/code/ddns-lambda.zip differ diff --git a/variables.tf b/variables.tf index 9b9c410..14448f1 100644 --- a/variables.tf +++ b/variables.tf @@ -50,6 +50,8 @@ variable "lambda_environment_variables" { TagKeyCname = "boc:dns:cname" TagKeyHostName = "boc:dns:name" TagKeyZone = "boc:dns:zone" + TagKeyPtrname = "boc:dns:ptrname" + TagKeyFlags = "boc:dns:flags" } }