Skip to content

Instantly share code, notes, and snippets.

@gaurish
Created August 7, 2020 02:07
Show Gist options
  • Save gaurish/b22ffb330ae594d72cdcffae34b1e822 to your computer and use it in GitHub Desktop.
Save gaurish/b22ffb330ae594d72cdcffae34b1e822 to your computer and use it in GitHub Desktop.
Scale down all services in a ECS cluster.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto3
from pprint import pprint
ecs_cluster = 'my_cluster_mame'
client = boto3.client('ecs', region_name='us-east-1')
def list_services(cluster):
response = client.list_services(cluster=cluster, maxResults=100,
launchType='EC2',
schedulingStrategy='REPLICA')
return [service for service in response['serviceArns']]
def desc_service(services):
return client.describe_services(cluster=ecs_cluster,
services=services)
def gen(service):
return {'name': service['serviceName'],
'desired_count': service['desiredCount']}
def update_service(service_name, desired_count):
client.update_service(cluster=ecs_cluster,
service=service['serviceName'],
desiredCount=desired_count)
services = list_services(ecs_cluster)
service_info = desc_service(services)
current_state = [gen(service) for service in service_info['services']]
for service in service_info['services']:
update_service(service, 0)
pprint(current_state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment