diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index 1b1357e..d0103ee 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.0b73' +VERSION = '1.2.0b74' # Read Env variables DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO') @@ -306,6 +306,20 @@ def lambda_handler( dns_data_tuple = namedtuple('DnsData', dns_data_fields) caller_response = [] +# if not an ec2 start or stop, skip processing, send SNS, and exit + action = evaluate_event_action(event) + if not action: + caller_response.append(f"Event structure not an aws.ec2 event, exiting") + if SNS_ENABLE: + sns_msg = {} + sns_msg['account_id'] = account_id + sns_msg['region'] = event['region'] + sns_msg['event'] = str(event) + sns_msg['context'] = str(context) + sns_msg['message'] = caller_response[-1] + publish_to_sns(sns_client, json.dumps(sns_msg)) + return caller_response + # Checking to make sure there is a dynamodb table named in the Env Variable tables = list_tables(dynamodb_client) @@ -3229,13 +3243,38 @@ 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 + :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. """ cluster_id = tags.get('aws:elasticmapreduce:job-flow-id', '') is_master = tags.get('aws:elasticmapreduce:instance-group-role', '') == 'MASTER' - return (is_master and cluster_id != '', cluster_id) + 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) + + +def evaluate_event_action(event): + """ + This takes the EventBridge event and returns a DNS action to take (ADD|REMOVE) based on running or not-running (stopping, terminating). If not an ec2 event, it returns None. + + :param dict(str) event: Event dict from handler + :return str: DNS action, either ADD (to create records) or REMOVE (to delete records) + e_source = event['source'] + action=None + try: + if e_source=='aws.ec2': + e_instance_id = event['detail']['instance-id'] + e_state = event['detail']['state'] + action="ADD" if e_state == "running" else "DELETE" + LOGGER.info(f"event_action ** {action} ** instance_id {instance_id} instance_state {e_state}") + else: + LOGGER.info(f"event_action unrecognized source {e_source}") + except: + LOGGER.error(f"event_action event structure cannot be parsed {lineno()}") + return action + ## # aws: elasticmapreduce: job - flow - id        j - 8O514K6HPIYZ diff --git a/code/ddns-lambda.zip b/code/ddns-lambda.zip index f49eae9..10d2272 100644 Binary files a/code/ddns-lambda.zip and b/code/ddns-lambda.zip differ