forked from gigigoapps/aws-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs_start_services.py
57 lines (49 loc) · 1.31 KB
/
ecs_start_services.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
#
# Modules Import
#
import boto3
import json
import os
#
# Variables Definition
#
ecs_cluster = os.environ['ECS_CLUSTER']
result = { 'startedServices': [ ] }
#
# Function to print the boto3 responses in JSON format
#
def json_response(response):
return json.dumps(
response,
default=str,
sort_keys=True,
indent=4,
separators=(',', ': ')
)
#
# Main
#
def lambda_handler(event, context):
print("\n##### Starting execution #####\n")
ecs = boto3.client('ecs')
# Get all services belonging to the ECS cluster
paginator = ecs.get_paginator('list_services')
response_iterator = paginator.paginate(cluster=ecs_cluster)
ecs_services = []
for page in response_iterator:
ecs_services.extend(page['serviceArns'])
for service in ecs_services:
print("\nScaling out '" + service + "' ECS service tasks to 1...")
response = ecs.update_service(
cluster=ecs_cluster,
service=service,
desiredCount=1
)
#print(json_response(response))
result['startedServices'].append(service)
print("\nFinal result:\n" + json_response(result) + "\n")
print("\n##### Execution finished #####\n")
return {
'statusCode': 200,
'body': json_response(result)
}