Interview Prep/Python for DevOps/Kubernetes, Docker & SSH clients

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.

All Python for DevOps questions Roadmap: Python for automation

More on Kubernetes, Docker & SSH clients