-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlambda_function.py
69 lines (51 loc) · 2.16 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
"""
A Lambda Function to set the desired count of running tasks
in a service based on a cluster's containter instances.
Designed to be triggered by a CloudWatch Event rule.
"""
from __future__ import print_function
import os
import boto3
def ecs_client():
return boto3.client("ecs")
def adjust_service_desired_count(ecs_client, cluster, service):
running_service = ecs_client.describe_services(cluster=cluster, services=[service])
if not running_service["services"]:
print("SKIP: Service not found in cluster {}".format(cluster))
return
desired_task_count = running_service["services"][0]["desiredCount"]
clusters = ecs_client.describe_clusters(clusters=[cluster])
registered_instances = clusters["clusters"][0]["registeredContainerInstancesCount"]
if desired_task_count != registered_instances:
print("Adjusting cluster '{}' to run {} tasks of service '{}'".format(
cluster, registered_instances, service
))
response = ecs_client.update_service(
cluster=cluster,
service=service,
desiredCount=registered_instances,
)
print(response)
return response
# Do nothing otherwise
print("SKIP: Cluster {} has {} desired tasks for {} registered instances.".format(
cluster, desired_task_count, registered_instances
))
return
def lambda_handler(event, context):
if not event:
raise ValueError("No event provided.")
if event["source"] != "aws.ecs":
raise ValueError("Function only supports input from events with a source type of: aws.ecs")
service = os.getenv('ECS_SERVICE_ARN')
if not service:
raise ValueError("Need to set `ECS_SERVICE_ARN` env var to serviceArn.")
# Determine if this event is one that we care about
if event["detail-type"] != "ECS Container Instance State Change":
print("SKIP: Function operates only on ECS Container Instance State Change events.")
return
# Valid event, and one we are interested in
cluster = event["detail"]["clusterArn"]
adjust_service_desired_count(ecs_client(), cluster, service)
print("DONE")