<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def run_instances(self, image_id, min_count, max_count, security_groups=None, key_name=None, instance_type=None, user_data=None, availability_zone=None, kernel_id=None, ramdisk_id=None, subnet_id=None, security_group_ids=None):
 """Run new instances. TODO: blockDeviceMapping, monitoring, subnetId """

        params = {"ImageId": image_id, "MinCount": str(min_count),
                  "MaxCount": str(max_count)}
        if key_name is not None:
            params["KeyName"] = key_name
        if subnet_id is not None:
            params["SubnetId"] = subnet_id
            if security_group_ids is not None:
                for i, id in enumerate(security_group_ids):
                    params["SecurityGroupId.%d" % (i + 1)] = id
            else:
                msg = "You must specify the security_group_ids with the subnet_id"
                raise ValueError(msg)
        elif security_groups is not None:
            for i, name in enumerate(security_groups):
                params["SecurityGroup.%d" % (i + 1)] = name
        else:
            msg = ("You must specify either the subnet_id and "
                   "security_group_ids or security_groups")
            raise ValueError(msg)
        if user_data is not None:
            params["UserData"] = b64encode(user_data)
        if instance_type is not None:
            params["InstanceType"] = instance_type
        if availability_zone is not None:
            params["Placement.AvailabilityZone"] = availability_zone
        if kernel_id is not None:
            params["KernelId"] = kernel_id
        if ramdisk_id is not None:
            params["RamdiskId"] = ramdisk_id
        query = self.query_factory(
            action="RunInstances", creds=self.creds, endpoint=self.endpoint,
            other_params=params)
        d = query.submit()
        return d.addCallback(self.parser.run_instances)