Skip to content

Instantly share code, notes, and snippets.

@chaseconey
Created November 29, 2017 22:22
Show Gist options
  • Save chaseconey/afc1d439b19c39e444993ba9054b0b96 to your computer and use it in GitHub Desktop.
Save chaseconey/afc1d439b19c39e444993ba9054b0b96 to your computer and use it in GitHub Desktop.
Update all ECS Agents for all Clusters in a given AWS Account
#!/usr/bin/env python
# A simple utility for updating all ecs agents in a given AWS account to the latest version
import boto3
import botocore.session
import argparse
from colorama import init, Fore, Style
init()
parser = argparse.ArgumentParser("update-ecs-agents")
parser.add_argument("--profile", help="The aws profile to use", default='default')
args = parser.parse_args()
# Tell Boto 3 to use that session by default
print(Fore.GREEN + "Setting profile to: " + args.profile)
boto3.setup_default_session(profile_name=args.profile)
ecs = boto3.client('ecs')
response = ecs.list_clusters()
for clusterArn in response.get('clusterArns', []):
print(Fore.WHITE + "Starting on cluster: " + clusterArn)
response = ecs.list_container_instances(cluster=clusterArn, status='ACTIVE')
for instanceArn in response.get('containerInstanceArns', []):
print(Fore.WHITE + "Updating agent on instance: " + instanceArn)
try:
response = ecs.update_container_agent(
cluster=clusterArn,
containerInstance=instanceArn
)
print(Fore.GREEN + "Update successfully applied")
except Exception:
print(Fore.YELLOW + "No update available")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment