Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Apr 27, 2023
1 parent 2b5d38f commit 03fcbd3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions code/ddns-lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
Binary file modified code/ddns-lambda.zip
Binary file not shown.

0 comments on commit 03fcbd3

Please sign in to comment.