Package: ec2-cluster
ec2-clusterSimple CLI and Python library to spin up and run shell commands on clusters of EC2 instances usingboto3andfabric. Multi-purpose, but created to make deep learning distributed training infrastructure easier. Also very useful for running performance tests across multiple EC2 instance types.QuickstartThis code will launch a cluster of EC2 instances, run the commandhostnameon all of them, return the results of the command and then tear down the cluster.importec2_clusterasec3withec3.infra.ConfigCluster("cluster.yaml")ascluster:sh=cluster.get_shell()results=sh.run_on_all("hostname")hostnames=[result.stdoutforresultinresults]Long-running tasksec2-clusteris also designed for long-running tasks where you may not want to keep your local machine awake for the full duration. The library relies on EC2 tags to keep track of EC2 instances, letting you interact with a cluster across sessions without needing an always-on control plane.Below is one way to launch complicated, long-running jobs and download the results at some later time.check_on_job()can be run in a different session or even a different machine thanlaunch_job()as long as they have the samecluster.yamland are using the same AWS account.importec2_clusterasec3deflaunch_job():cluster=ec3.infra.ConfigCluster("cluster.yaml")cluster.launch(verbose=True)sh=cluster.get_shell()sh.copy_from_local_to_all("job_script.py","job_script.py")sh.run_on_all("python job_script.py > job.log 2>&1 &")# Launch script as background processdefcheck_on_job():cluster=ec3.infra.ConfigCluster("cluster.yaml")sh=cluster.get_shell()statuses=sh.run_on_all("tail -n 1 job.log",hide=True)forstatusinstatuses:ifstatus.stdout.rstrip("\n")!="job_script.py complete":print("Job is not yet complete on all instances")returnprint("Job is finally done on all instances!")sh.copy_from_all_to_local("job.log","./results/")cluster.terminate(verbose=True)This will create a directory on your local machine:results/
├── 0
│   ├── ip.txt
│   └── job.log
├── 1
│   ├── ip.txt
│   └── job.log
└── 3
    ├── ip.txt
    └── job.logOverviewec2-clusteris designed for simple distributed tasks where Kubernetes is overkill. There is no setup required other than the ability to launch EC2 instances withboto3and the ability to SSH to those instances (only a requirement if you want to run commands on them).ec2-clusterprovides the ability to launch a cluster, to retrieve IP addresses for all nodes/nodes in the cluster, to delete the cluster and to execute commands on some or all of the instances.Unlike like most cluster management tools, all cluster management inec2-clusterhappens client-side.ec2-clustercreates EC2 instances and gives them globally unique names (using the EC2 Name tag) which is later used to identify which instances are part of a given cluster.ec2-clusterthen provides convenience classes to run commands on some or all of the instances via SSH (using thefabriclibrary).Other benefitsResilient to EC2 capacity limits. If instances are not available,ec2-clusterwill retry until the all nodes in the cluster are created or until the user-set timeout is reached.Easy to quickly launch duplicate clusters for parallel training runs.Can write orchestration logic that needs to be run when launching a cluster, e.g. enabling passwordless ssh between all instances for Horovod-based trainingIterative, not disruptive, improvements on the common methodology of manually launching EC2 instances, ssh-ing to them, configuring environments by hand and running scriptsUsageec2-clustercan be consumed in two ways:A CLI for launching, describing and deleting clusters.A python library for scripting.This library has three main components:Linfra: creating cluster infrastructureorch: orchestrating simple runtime cluster configuration (e.g. generate a hostfile with runtime IPs)control: running commands on the clusterCLI Quick StartLibrary Quick Start