diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index 76c7559..df64417 100755 --- a/code/ddns-lambda.py +++ b/code/ddns-lambda.py @@ -72,7 +72,7 @@ LOGGER = logging.getLogger() ACCOUNT = None REGION = None -VERSION = '0.1.0' +VERSION = '0.1.1' # Adjust the logging level [logging.INFO, logging.DEBUG, logging.WARNING, etc] LOGGER.setLevel(logging.DEBUG) @@ -141,7 +141,7 @@ def get_caller_account_id(): :return str: AWS Account ID """ try: - return boto3.client('sts').get_caller_identity()['Account']) + return boto3.client('sts').get_caller_identity()['Account'] except ClientError as err: print("Unexpected error: %s" % err) @@ -149,9 +149,9 @@ def get_caller_account_id(): def lambda_handler( event, context, - dynamodb_client = get_dynamodb_client(), - compute = get_ec2_client(), - route53 = get_route53_client() + dynamodb_client=get_dynamodb_client(), + compute=get_ec2_client(), + route53=get_route53_client() ): @@ -171,9 +171,9 @@ def lambda_handler( LOGGER.info("event: %s", str(event) + lineno()) LOGGER.info("context: %s", str(context) + lineno()) - caller_response=[] + caller_response = [] # Checking to make sure there is a dynamodb table named in the Env Variable - tables=list_tables(dynamodb_client) + tables = list_tables(dynamodb_client) LOGGER.info("tables: %s", str(tables)) if DDBNAME in tables['TableNames']: @@ -184,15 +184,15 @@ def lambda_handler( # Set variables # Get the state from the Event stream - state=event['detail']['state'] + state = event['detail']['state'] LOGGER.debug("instance state: %s", str(state) + lineno()) # Get the instance id, region, and tag collection - instance_id=event['detail']['instance-id'] + instance_id = event['detail']['instance-id'] LOGGER.debug("instance id: %s", str(instance_id) + lineno()) - region=event['region'] + region = event['region'] LOGGER.debug("region: %s", str(region) + lineno()) - account_id=get_caller_account_id() + account_id = get_caller_account_id() LOGGER.debug("account_id: %s", str(account_id) + lineno()) # Only doing something if the state is running @@ -200,7 +200,7 @@ def lambda_handler( LOGGER.debug("sleeping for maximum {} seconds {}".format(SLEEPTIME, lineno())) # wait increment and wait until maximum sleeptime - i=1 + i = 1 while i < SLEEPTIME: LOGGER.debug("waiting count: %s", str(i) + lineno()) time.sleep(1) @@ -208,12 +208,12 @@ def lambda_handler( try: # Get instance information - instance=get_instances(compute, instance_id) + instance = get_instances(compute, instance_id) - t_private_ip=instance['Reservations'][0]['Instances'][0]['PrivateIpAddress'] - t_private_dns_name=instance['Reservations'][0]['Instances'][0]['PrivateDnsName'] - t_subnet_id=instance['Reservations'][0]['Instances'][0]['SubnetId'] - t_vpc_id=instance['Reservations'][0]['Instances'][0]['VpcId'] + t_private_ip = instance['Reservations'][0]['Instances'][0]['PrivateIpAddress'] + t_private_dns_name = instance['Reservations'][0]['Instances'][0]['PrivateDnsName'] + t_subnet_id = instance['Reservations'][0]['Instances'][0]['SubnetId'] + t_vpc_id = instance['Reservations'][0]['Instances'][0]['VpcId'] # if key attributes are found, then break out of the loop if all([t_private_ip, t_private_dns_name, t_subnet_id, t_vpc_id]): @@ -229,9 +229,9 @@ def lambda_handler( # Remove null values from the response. You cannot save a dict/JSON # document in DynamoDB if it contains null values LOGGER.debug("instance: %s", str(instance) + lineno()) - instance=remove_empty_from_dict(instance) - instance_dump=json.dumps(instance, default = json_serial) - instance_attributes=json.loads(instance_dump) + instance = remove_empty_from_dict(instance) + instance_dump = json.dumps(instance, default=json_serial) + instance_attributes = json.loads(instance_dump) LOGGER.debug("instance_attributes: %s", str(instance_attributes) + lineno()) LOGGER.debug("trying to put instance information in " "dynamo table %s", str(instance_attributes) + lineno()) @@ -605,13 +605,13 @@ def lambda_handler( " %s", str(valid_dns_zones) + lineno()) # create the TXT heritage record - heritage = initialize_heritage(HERITAGE_TAG,VERSION, - { - 'instance_id': instance_id, - 'account_id': account_id, - 'region': region - } ) - add_heritage_item_timestamp(heritage,'create_time') + heritage = initialize_heritage(HERITAGE_TAG, VERSION, + { + 'instance_id': instance_id, + 'account_id': account_id, + 'region': region + }) + add_heritage_item_timestamp(heritage, 'create_time') heritage_value = format_heritage(heritage) # txt=format_heritage(h) @@ -637,24 +637,24 @@ def lambda_handler( ' with value: ' + str(private_ip)) - if len(heritage)>0: - LOGGER.debug("Creating heritage TXT resource records %s", lineno()) - create_resource_record( - route53, - final_hosted_zone_id, - final_private_hostname, - final_hosted_zone_name, - 'TXT', - heritage_value - ) - - caller_response.append('Created TXT record in zone id: ' + - str(final_hosted_zone_id) + - ' for hosted zone ' + - str(final_private_hostname) + '.' + - str(final_hosted_zone_name) + - ' with value: ' + - str(heritage_value)) + if len(heritage) > 0: + LOGGER.debug("Creating heritage TXT resource records %s", lineno()) + create_resource_record( + route53, + final_hosted_zone_id, + final_private_hostname, + final_hosted_zone_name, + 'TXT', + heritage_value + ) + + caller_response.append('Created TXT record in zone id: ' + + str(final_hosted_zone_id) + + ' for hosted zone ' + + str(final_private_hostname) + '.' + + str(final_hosted_zone_name) + + ' with value: ' + + str(heritage_value)) if reverse_zone_associated: create_resource_record( @@ -673,24 +673,23 @@ def lambda_handler( 'in-addr.arpa with value: ' + str(final_private_dns_name)) - if len(heritage)>0: - create_resource_record( - route53, - reverse_lookup_zone_id, - reversed_ip_address, - 'in-addr.arpa', - 'TXT', - heritage_value - ) - - caller_response.append('Created TXT reverse record in zone id: ' + - str(reverse_lookup_zone_id) + - ' for hosted zone ' + - str(reversed_ip_address) + - 'in-addr.arpa with value: ' + - str(heritage_value)) - - + if len(heritage) > 0: + create_resource_record( + route53, + reverse_lookup_zone_id, + reversed_ip_address, + 'in-addr.arpa', + 'TXT', + heritage_value + ) + + caller_response.append('Created TXT reverse record in zone id: ' + + str(reverse_lookup_zone_id) + + ' for hosted zone ' + + str(reversed_ip_address) + + 'in-addr.arpa with value: ' + + str(heritage_value)) + except BaseException as err: LOGGER.info("unexpected error. %s\n", str(err) + lineno()) @@ -716,26 +715,26 @@ def lambda_handler( ' with value: ' + str(private_ip)) - if len(heritage)>0: - # pause 1 before deleting to avoid API limit - time.sleep(1) - delete_resource_record( - route53, - final_hosted_zone_id, - final_private_hostname, - final_hosted_zone_name, - 'TXT', - heritage_value - ) - - caller_response.append('Deleted TXT record in zone id: ' + - str(final_hosted_zone_id) + - ' for hosted zone ' + - str(final_private_hostname) + '.' + - str(final_hosted_zone_name) + - ' with value: ' + - str(heritage_value)) - + if len(heritage) > 0: + # pause 1 before deleting to avoid API limit + time.sleep(1) + delete_resource_record( + route53, + final_hosted_zone_id, + final_private_hostname, + final_hosted_zone_name, + 'TXT', + heritage_value + ) + + caller_response.append('Deleted TXT record in zone id: ' + + str(final_hosted_zone_id) + + ' for hosted zone ' + + str(final_private_hostname) + '.' + + str(final_hosted_zone_name) + + ' with value: ' + + str(heritage_value)) + # pause 1 before deleting to avoid API limit time.sleep(1) delete_resource_record( @@ -755,26 +754,26 @@ def lambda_handler( ' with value: ' + str(final_private_dns_name)) - if len(heritage)>0: - # pause 1 before deleting to avoid API limit - time.sleep(1) - delete_resource_record( - route53, - reverse_lookup_zone_id, - reversed_ip_address, - 'in-addr.arpa', - 'TXT', - heritage_value - ) - - caller_response.append('Deleted PTR record in zone id: ' + - str(reverse_lookup_zone_id) + - ' for hosted zone ' + - str(reversed_ip_address) + - str(private_dns_name) + - ' with value: ' + - str(heritage_value)) - + if len(heritage) > 0: + # pause 1 before deleting to avoid API limit + time.sleep(1) + delete_resource_record( + route53, + reverse_lookup_zone_id, + reversed_ip_address, + 'in-addr.arpa', + 'TXT', + heritage_value + ) + + caller_response.append('Deleted PTR record in zone id: ' + + str(reverse_lookup_zone_id) + + ' for hosted zone ' + + str(reversed_ip_address) + + str(private_dns_name) + + ' with value: ' + + str(heritage_value)) + except BaseException as err: LOGGER.debug("%s", str(err) + lineno()) @@ -814,25 +813,25 @@ def lambda_handler( ' with value: ' + str(final_private_dns_name)) - if len(heritage)>0: - create_resource_record( - route53, - cname_domain_suffix_id, - TXT_RR_PREFIX + '.' + cname_host_name, - cname_domain_suffix, - 'TXT', - heritage_value - ) - - caller_response.append('Created TXT for CNAME record in zone id: ' + - str(cname_domain_suffix_id) + - ' for hosted zone ' + - str(TXT_RR_PREFIX) + '.' + - str(cname_host_name) + '.' + - str(cname_domain_suffix) + - ' with value: ' + - str(heritage_value)) - + if len(heritage) > 0: + create_resource_record( + route53, + cname_domain_suffix_id, + TXT_RR_PREFIX + '.' + cname_host_name, + cname_domain_suffix, + 'TXT', + heritage_value + ) + + caller_response.append('Created TXT for CNAME record in zone id: ' + + str(cname_domain_suffix_id) + + ' for hosted zone ' + + str(TXT_RR_PREFIX) + '.' + + str(cname_host_name) + '.' + + str(cname_domain_suffix) + + ' with value: ' + + str(heritage_value)) + except BaseException as err: LOGGER.debug("%s", str(err) + lineno()) else: @@ -858,25 +857,25 @@ def lambda_handler( ' with value: ' + str(final_private_dns_name)) - if len(heritage)>0: - delete_resource_record( - route53, - cname_domain_suffix_id, - TXT_RR_PREFIX + '.' + cname_host_name, - cname_domain_suffix, - 'TXT', - heritage_value - ) - - caller_response.append('Deleted TXT for CNAME record in zone id: ' + - str(cname_domain_suffix_id) + - ' for hosted zone ' + - str(TXT_RR_PREFIX) + '.' + - str(cname_host_name) + '.' + - str(cname_domain_suffix) + - ' with value: ' + - str(heritage_value)) - + if len(heritage) > 0: + delete_resource_record( + route53, + cname_domain_suffix_id, + TXT_RR_PREFIX + '.' + cname_host_name, + cname_domain_suffix, + 'TXT', + heritage_value + ) + + caller_response.append('Deleted TXT for CNAME record in zone id: ' + + str(cname_domain_suffix_id) + + ' for hosted zone ' + + str(TXT_RR_PREFIX) + '.' + + str(cname_host_name) + '.' + + str(cname_domain_suffix) + + ' with value: ' + + str(heritage_value)) + except BaseException as err: LOGGER.debug("%s", str(err) + lineno()) diff --git a/code/ddns-lambda.zip b/code/ddns-lambda.zip index 157de09..8ef42fc 100644 Binary files a/code/ddns-lambda.zip and b/code/ddns-lambda.zip differ