EC2: run_instances launches MinCount instances, MaxCount is ignored
Moto 5.1.18, boto3 1.34.162, Python 3.12
run_instances with MinCount=1, MaxCount=3 creates a single instance:
import boto3
from moto import mock_aws
@mock_aws
def repro():
client = boto3.client("ec2", region_name="us-east-1")
resp = client.run_instances(
ImageId="ami-12c6146b", MinCount=1, MaxCount=3
)
print(len(resp["Instances"])) # 1, expected 3
repro()On real AWS the request launches as many instances as it can, between MinCount and MaxCount — so with capacity available this returns 3 instances, and fewer (but at least MinCount) only when capacity runs out. See "How instance launches work" in the RunInstances docs: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RunInstances.html
Moto only reads MinCount and uses it as the instance count, MaxCount is parsed but never looked at:
https://github.com/getmoto/moto/blob/master/moto/ec2/responses/instances.py#L49
min_count = int(self._get_param("MinCount", if_none="1"))
...
new_reservation = self.ec2_backend.run_instances(
image_id, min_count, user_data, security_group_names, **kwargs
)This goes unnoticed because pretty much everyone calls run_instances with MinCount == MaxCount (moto's own test suite does too, e.g. test_add_servers). We hit it with a batch launcher that intentionally uses MinCount=1, MaxCount=N and handles partial fulfillment — under moto every batch quietly degrades to a single instance, so the multi-instance code path was never actually tested.
Since moto has no capacity limits to hit, I'd expect it to launch MaxCount instances. Happy to send a PR if you agree that's the right behaviour.
Source: getmoto/moto