Skip to content

Commit 6dd1b57

Browse files
committed
better handle empty ip list and auto trim spaces in tag values
1 parent 4183ef4 commit 6dd1b57

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

terraform/lambda/lambda_asg_updateroute53_tag.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
log_level = logging.INFO if not env_level else env_level
1616
logger.setLevel(log_level)
1717

18-
version="2022040303"
18+
version="2024112205"
1919

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

127128
def get_asg_public_ips(asg_client,ec2_client,asg_name):
@@ -135,14 +136,17 @@ def get_asg_public_ips(asg_client,ec2_client,asg_name):
135136
for reservation in ec2_client.describe_instances(InstanceIds = instance_ids)['Reservations']:
136137
for instance in reservation['Instances']:
137138
if instance['State']['Name'] == 'running':
138-
servers.append({'Value': instance['PublicIpAddress']})
139+
if 'PublicIpAddress' in instance:
140+
servers.append({'Value': instance['PublicIpAddress']})
139141
return servers
140142

141143
def get_asg_dns_tags(asg_client,asg_name,region):
142144
r=asg_client.describe_tags(Filters=[{'Name':'auto-scaling-group','Values':[asg_name]},{'Name':'key','Values':['splunkdnszone','splunkdnsnames','splunkdnsprefix']}])
143145
for tag in r['Tags']:
144146
k=tag['Key']
145147
v=tag['Value']
148+
# strip to remove extra spaces around values
149+
v=v.strip()
146150
print (f"key={k},Value={v}")
147151
if k == 'splunkdnszone':
148152
zone=v

0 commit comments

Comments
 (0)