Skip to content

Instantly share code, notes, and snippets.

@dispera
Last active July 2, 2019 13:37
Show Gist options
  • Save dispera/e2b91c50fb6bd87a1906132848191640 to your computer and use it in GitHub Desktop.
Save dispera/e2b91c50fb6bd87a1906132848191640 to your computer and use it in GitHub Desktop.
Script to update ALL the ECS agents on the specified regions
#!/usr/bin/python3
#
# This will update ALL the ECS agents on the specified regions
# Requires: pip3 install boto3
#
# You can use this with https://github.com/mheffner/awsam
# for easy AWS vars management, eg:
# aem use pingdom-prod
# aenv python3 ecs-agents-updater.py
import boto3
regions = ['eu-west-1','us-east-1']
updated_agents = 0
for region in regions:
client = boto3.client('ecs',region_name=region)
ecs_clusters = client.list_clusters()
print('Attemping to update ECS agents on {}..'.format(region))
# Iterate through the ECS clusters
for ecs_cluster in ecs_clusters['clusterArns']:
instances = client.list_container_instances(
cluster=ecs_cluster,
status='ACTIVE'
)
# Iterate through the ECS Instances in the cluster
for instance in instances['containerInstanceArns']:
try:
# Attempt to Update the ECS Agent on the instance
updater = client.update_container_agent(
cluster=ecs_cluster,
containerInstance=instance
)
except client.exceptions.NoUpdateAvailableException:
pass
except client.exceptions.UpdateInProgressException:
print('Update is already in progress on instance {}'.format(instance))
else:
print('Updating ECS agent on instance {}'.format(instance))
updated_agents += 1
print('Total number of ECS Agents updated on region {}: {}'.format(region,updated_agents))
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment