Skip to content

Commit

Permalink
better handle empty ip list and auto trim spaces in tag values
Browse files Browse the repository at this point in the history
  • Loading branch information
araman-m committed Nov 22, 2024
1 parent 4183ef4 commit 6dd1b57
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions terraform/lambda/lambda_asg_updateroute53_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
log_level = logging.INFO if not env_level else env_level
logger.setLevel(log_level)

version="2022040303"
version="2024112205"

# dont set this too low or the entry could be invalid before being used at all ...
ttl = 300
Expand Down Expand Up @@ -121,7 +121,8 @@ def get_asg_private_ips(asg_client,ec2_client,asg_name):
for reservation in ec2_client.describe_instances(InstanceIds = instance_ids)['Reservations']:
for instance in reservation['Instances']:
if instance['State']['Name'] == 'running':
servers.append({'Value': instance['PrivateIpAddress']})
if 'PrivateIpAddress' in instance:
servers.append({'Value': instance['PrivateIpAddress']})
return servers

def get_asg_public_ips(asg_client,ec2_client,asg_name):
Expand All @@ -135,14 +136,17 @@ def get_asg_public_ips(asg_client,ec2_client,asg_name):
for reservation in ec2_client.describe_instances(InstanceIds = instance_ids)['Reservations']:
for instance in reservation['Instances']:
if instance['State']['Name'] == 'running':
servers.append({'Value': instance['PublicIpAddress']})
if 'PublicIpAddress' in instance:
servers.append({'Value': instance['PublicIpAddress']})
return servers

def get_asg_dns_tags(asg_client,asg_name,region):
r=asg_client.describe_tags(Filters=[{'Name':'auto-scaling-group','Values':[asg_name]},{'Name':'key','Values':['splunkdnszone','splunkdnsnames','splunkdnsprefix']}])
for tag in r['Tags']:
k=tag['Key']
v=tag['Value']
# strip to remove extra spaces around values
v=v.strip()
print (f"key={k},Value={v}")
if k == 'splunkdnszone':
zone=v
Expand Down

0 comments on commit 6dd1b57

Please sign in to comment.