How do you run a command on many EC2 instances without SSH?
IntermediateConceptKubernetes, Docker & SSH clients
Answer
Use SSM Run Command via boto3:
ssm = boto3.client("ssm")
cmd = ssm.send_command(Targets=[{"Key": "tag:Role", "Values": ["web"]}],
DocumentName="AWS-RunShellScript",
Parameters={"commands": ["systemctl is-active nginx"]},
MaxConcurrency="10%", MaxErrors="1")
cid = cmd["Command"]["CommandId"]
# later: ssm.list_command_invocations(CommandId=cid, Details=True)
No open port 22, no keys, IAM-controlled and logged in CloudTrail; output can go to S3 or CloudWatch Logs. Instances need the SSM agent and an instance profile with AmazonSSMManagedInstanceCore.
More on Kubernetes, Docker & SSH clients
How does the official Kubernetes Python client authenticate?BeginnerHow do you list pods with high restart counts using the Python client?BeginnerHow do you patch or scale a Deployment with the Python client?IntermediateHow does `watch.Watch()` work, and what is a `410 Gone`?AdvancedHow do you apply a YAML manifest from Python?AdvancedHow do you use the Docker SDK for Python?Beginner