Skip to content
This repository was archived by the owner on Jun 5, 2018. It is now read-only.

Commit 4ba73c5

Browse files
committed
lambda: fix support for multiple clusters
1 parent 61783f1 commit 4ba73c5

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

lambda_health_check.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ def search_task(port, ec2Instance, service, list_tasks):
5959
return task
6060

6161
def search_ecs_task(ip, port, service, ecs_data):
62-
ec2Instance = search_ecs_instance(ip, ecs_data['ec2Instances'])
63-
if ec2Instance != None:
64-
task = search_task(port, ec2Instance, service, ecs_data['tasks'])
65-
if task != None:
66-
return task
67-
62+
for data in ecs_data:
63+
ec2Instance = search_ecs_instance(ip, data['ec2Instances'])
64+
if ec2Instance != None:
65+
task = search_task(port, ec2Instance, service, data['tasks'])
66+
if task != None:
67+
return [data, task]
68+
69+
return [None, None]
70+
6871
def delete_route53_record(record, comment):
6972
route53.change_resource_record_sets(
7073
HostedZoneId=hostedZoneId,
@@ -84,7 +87,7 @@ def process_records(response, ecs_data):
8487
for rr in record['ResourceRecords']:
8588
[ip, port] = get_ip_port(rr)
8689
if ip != None:
87-
task=search_ecs_task(ip, port, '.'.join(record['Name'].split('.')[0:2]), ecs_data)
90+
[cluster_data, task] = search_ecs_task(ip, port, '.'.join(record['Name'].split('.')[0:2]), ecs_data)
8891
if task == None:
8992
delete_route53_record(record, 'Service Discovery Health Check failed')
9093
print("Record %s deleted" % rr)
@@ -101,21 +104,25 @@ def process_records(response, ecs_data):
101104
print("Record %s deleted" % rr)
102105
if task != None:
103106
ecs.stop_task(
104-
cluster=ecs_data['instanceArns'][ecs_data['tasks'][task]['instance']]['cluster'],
105-
task=task,
106-
reason='Service Discovery Health Check failed'
107+
cluster = cluster_data['instanceArns'][cluster_data['tasks'][task]['instance']]['cluster'],
108+
task = task,
109+
reason = 'Service Discovery Health Check failed'
107110
)
108111
print("Task %s stopped" % task)
109112

110113
elif record['Type'] == 'A':
111114
if 'ResourceRecords' in record:
112115
for rr in record['ResourceRecords']:
113-
if (ecs_data['clusterPrivateIPs'].count(rr['Value']) == 0) and (record['Name'] == ('ip-' + rr['Value'].replace('.', '-') + '.' + domain + '.')):
116+
count = 0
117+
for data in ecs_data:
118+
count += data['clusterPrivateIPs'].count(rr['Value'])
119+
120+
if count == 0 and (record['Name'] == ('ip-' + rr['Value'].replace('.', '-') + '.' + domain + '.')):
114121
delete_route53_record(record, 'Instance no longer exists in cluster')
115122
print("Record %s deleted" % rr['Value'])
116123
break
117124

118-
elif ecs_data['clusterPrivateIPs'].count(rr['Value']) > 1:
125+
if count > 1:
119126
print("ERROR: IP %s exists on multiple cluster instances" % rr['Value'])
120127
break
121128

@@ -148,11 +155,12 @@ def get_service_for_port(port, environment):
148155
return ""
149156

150157
def get_ecs_data():
151-
list_ec2_instances = {}
152-
list_ecs_private_ips = []
153-
list_instance_arns = {}
154-
list_tasks = {}
158+
data = []
155159
for cluster_name in ecs_clusters:
160+
list_ec2_instances = {}
161+
list_ecs_private_ips = []
162+
list_instance_arns = {}
163+
list_tasks = {}
156164
response = ecs.list_container_instances(cluster=cluster_name)
157165
for instance_arn in response['containerInstanceArns']:
158166
list_instance_arns[instance_arn] = {'cluster': cluster_name}
@@ -182,8 +190,10 @@ def get_ecs_data():
182190
if service != "":
183191
list_tasks[task['taskArn']]['containers'].append({'service': service, 'port': str(networkBinding['hostPort'])})
184192

185-
return {'instanceArns': list_instance_arns, 'ec2Instances': list_ec2_instances, 'tasks': list_tasks, 'clusterPrivateIPs': list_ecs_private_ips}
186-
193+
data.append({'instanceArns': list_instance_arns, 'ec2Instances': list_ec2_instances, 'tasks': list_tasks, 'clusterPrivateIPs': list_ecs_private_ips})
194+
195+
return data
196+
187197
def lambda_handler(event, context):
188198
#print('Starting')
189199

0 commit comments

Comments
 (0)