Skip to content

Commit

Permalink
- code 2.0.4
Browse files Browse the repository at this point in the history
  - add context info and event id to SNS messages
  - add heritage mismatch information to output
  • Loading branch information
badra001 committed May 17, 2023
1 parent 5b2e4e0 commit 56b6979
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,8 @@
* 2.0.3 -- 2023-05-11
- code 2.0.3
- fix emr cluster detection

* 2.0.4 -- 2023-05-17
- code 2.0.4
- add context info and event id to SNS messages
- add heritage mismatch information to output
67 changes: 60 additions & 7 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 = '2.0.3'
VERSION = '2.0.4'

# Read Env variables
DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO')
Expand Down Expand Up @@ -319,6 +319,10 @@ def lambda_handler(
sns_msg['region'] = event['region']
sns_msg['event'] = str(event)
sns_msg['context'] = str(context)
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['message'] = caller_response[-1]
publish_to_sns(sns_client, json.dumps(sns_msg))
return caller_response
Expand All @@ -338,6 +342,10 @@ def lambda_handler(
sns_msg['instance_id'] = event['detail']['instance-id']
sns_msg['account_id'] = account_id
sns_msg['region'] = event['region']
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['message'] = 'DynamoDB table does not exist: ' + DDBNAME
publish_to_sns(sns_client, json.dumps(sns_msg))
return caller_response
Expand Down Expand Up @@ -511,6 +519,10 @@ def lambda_handler(
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = account_id
sns_msg['region'] = region
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'ec2'
sns_msg['boto3_method'] = 'describe_vpc_attribute'
sns_msg['message'] = "You have to enable DNS hostnames to use Route 53 private hosted zones. DNS hostnames disabled for " + vpc_id
Expand All @@ -529,6 +541,10 @@ def lambda_handler(
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = account_id
sns_msg['region'] = region
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'ec2'
sns_msg['boto3_method'] = 'describe_vpc_attribute'
sns_msg['message'] = "You have to enabled DNS support to use Route 53 private hosted zones. DNS support disabled for " + vpc_id
Expand Down Expand Up @@ -624,6 +640,10 @@ def lambda_handler(
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = account_id
sns_msg['region'] = region
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'ec2'
sns_msg['boto3_method'] = 'describe_vpcs'
sns_msg['message'] = 'No DHCP option set assigned to this VPC: ' + vpc_id
Expand Down Expand Up @@ -1259,6 +1279,10 @@ def new_list_hosted_zones(client, instance_id):
sns_msg = {}
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = get_caller_account_id()
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'route53'
sns_msg['boto3_method'] = 'list_hosted_zones'
sns_msg['message'] = 'list_hosted_zones timed out'
Expand Down Expand Up @@ -1336,6 +1360,10 @@ def new_list_hosted_zones_by_vpc(client, instance_id, vpc_id, region):
sns_msg['vpc_id'] = vpc_id
sns_msg['region'] = region
sns_msg['account_id'] = get_caller_account_id()
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'route53'
sns_msg['boto3_method'] = 'list_hosted_zones_by_vpc'
sns_msg['message'] = 'list_hosted_zones_by_vpc timed out'
Expand Down Expand Up @@ -1773,6 +1801,10 @@ def new_change_resource_recordset(oclient, instance_id, zone_id, host_name, host
sns_msg = {}
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = get_caller_account_id()
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'route53'
sns_msg['boto3_method'] = 'change_resource_record_sets'
sns_msg['message'] = 'change_resource_record_sets could not UPSERT record'
Expand Down Expand Up @@ -1989,6 +2021,10 @@ def new_get_resource_record(oclient, instance_id, zone_id, host_name, hosted_zon
sns_msg = {}
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = get_caller_account_id()
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'route53'
sns_msg['boto3_method'] = 'list_resource_record_sets'
sns_msg['message'] = 'list_resource_record_sets timed out'
Expand Down Expand Up @@ -2119,6 +2155,10 @@ def new_delete_resource_record(oclient, instance_id, zone_id, host_name, hosted_
sns_msg = {}
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = get_caller_account_id()
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'route53'
sns_msg['boto3_method'] = 'change_resource_record_sets'
sns_msg['message'] = 'change_resource_record_sets could not DELETE record'
Expand Down Expand Up @@ -2581,6 +2621,10 @@ def new_get_hosted_zone_properties(client, instance_id, zone_id):
sns_msg = {}
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = get_caller_account_id()
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['client'] = 'route53'
sns_msg['boto3_method'] = 'get_hosted_zone'
sns_msg['message'] = 'get_hosted_zone timed out'
Expand Down Expand Up @@ -2898,23 +2942,27 @@ def process_delete_records(route53, instance_id, zone_id,

# Return the dictionary of the value with comma separated
heritage = parse_heritage(heritage_value)
LOGGER.debug("heritage parsed data in string format: %s", str(heritage) + lineno())
LOGGER.debug(f"heritage parsed data in string format: '{heritage}' {lineno()}")

# check if the TXT record was created by the Lambda as match instance-id
if verify_heritage_owner(heritage, HERITAGE_TAG):
LOGGER.debug("TXT record was created by Lambda DDNS %s",
HERITAGE_TAG + lineno())
LOGGER.debug(
f"TXT record was created by Lambda DDNS tag application '{HERITAGE_TAG}' {lineno()}")
heritage_own = True
else:
LOGGER.info("TXT record was not created by Lambda DDNS %s",
HERITAGE_TAG + lineno())
LOGGER.info(
f"TXT record was not created by Lambda DDNS tag application '{HERITAGE_TAG}' {lineno()}")
LOGGER.info(f"found_record is '{heritage_value}' {lineno()}")
LOGGER.info(f"parsed heritage is is '{heritage}' {lineno()}")
heritage_own = False

if compare_heritage(heritage, 'instance_id', instance_id):
LOGGER.debug("TXT record matches instance_id: %s", instance_id + lineno())
heritage_instance_match = True
else:
LOGGER.info("TXT record does not match instance_id: %s", instance_id + lineno())
LOGGER.info(f"found_record is '{heritage_value}' {lineno()}")
LOGGER.info(f"parsed heritage is is '{heritage}' {lineno()}")
heritage_instance_match = False

# delete A/PTR/AAAA/CNAME record
Expand Down Expand Up @@ -2980,6 +3028,10 @@ def process_delete_records(route53, instance_id, zone_id,
if SNS_ENABLE:
try:
sns_msg = {}
sns_msg['event_id'] = event['id']
sns_msg['aws_request_id'] = context.aws_request_id
sns_msg['log_group_name'] = context.log_group_name
sns_msg['log_stream_name'] = context.log_stream_name
sns_msg['instance_id'] = instance_id
sns_msg['account_id'] = get_caller_account_id()
sns_msg['message'] = 'TXT record does not match. Will not delete the A/PTR/CNAME/AAAA record.'
Expand Down Expand Up @@ -3096,7 +3148,8 @@ def tags_to_dict(tags):

tag_dict = {}
if len(tags) > 0:
tag_dict = {tag.get('Key', '').lstrip().rstrip(): tag.get('Value', '') for tag in tags}
tag_dict = {tag.get('Key', '').lstrip().rstrip()
: tag.get('Value', '') for tag in tags}
return tag_dict


Expand Down
Binary file modified code/ddns-lambda.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion version.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
locals {
_module_version = "2.0.3"
_module_version = "2.0.4"
}

0 comments on commit 56b6979

Please sign in to comment.