Skip to content

Commit

Permalink
fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Feb 22, 2022
1 parent d37ecd6 commit 3f21df5
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 141 deletions.
281 changes: 140 additions & 141 deletions code/ddns-lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -141,17 +141,17 @@ 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)


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()


):
Expand All @@ -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']:
Expand All @@ -184,36 +184,36 @@ 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
if state == 'running':
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)
i += 1

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]):
Expand All @@ -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())
Expand Down Expand Up @@ -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)

Expand All @@ -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(
Expand All @@ -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())

Expand All @@ -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(
Expand All @@ -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())

Expand Down Expand Up @@ -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:
Expand All @@ -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())

Expand Down
Binary file modified code/ddns-lambda.zip
Binary file not shown.

0 comments on commit 3f21df5

Please sign in to comment.