I am trying to run a SSM command on more than 50 EC2 instances of my fleet. By using AWS boto3’s SSM client, I am running a specific command on my nodes. My code is given below. After running the code, an unexpected error is showing up.
# running ec2 instances
instances = client.describe_instances()
instance_ids = [inst["InstanceId"] for inst in instances] # might contain more than 50 instances
# run command
run_cmd_resp = ssm_client.send_command(
Targets=[
{"Key": "InstanceIds", "Values": inst_ids_all},
],
DocumentName="AWS-RunShellScript",
DocumentVersion="1",
Parameters={
"commands": ["#!/bin/bash", "ls -ltrh", "# some commands"]
}
)
On executing this, getting below error
An error occurred (ValidationException) when calling the SendCommand operation: 1 validation error detected: Value '[...91 instance IDs...]' at 'targets.1.member.values' failed to satisfy constraint: Member must have length less than or equal to 50.
How do I run the SSM command my whole fleet?
2
Answers
Finally after @Rohan Kishibe's answer, I tried to implement below batched execution for the SSM runShellScript.
In above way, the total number of instance IDs will be distributed in batches and then executed accordingly. One can also save the Command IDs and batch instance IDs in a mapping for future usage.
As shown in the error message and boto3 documentation (link), the number of instances in one
send_command
call is limited up to 50. To run the SSM command for all instances, splitting the original list into 50 each could be a solution.FYI: If your account has a fair amount of instances,
describe_instances()
can’t retrieve all instance info in one api call, so it would be better to check whetherNextToken
is in response.ref: How do you use "NextToken" in AWS API calls