There's actually an AWS API that does that. That's create-fleet in 'instant' mode. Docs are here:
https://docs.aws.amazon.com/cli/latest/reference/ec2/create-...
In short, you create a request specifying your CPU and memory constraints (you can also specify other constraints like processor manufacturer, memory per vCPU, etc). Then select lowest-price allocation strategy, and fleet "--type instant". Fleet will make a synchronous request provisioning the currently cheapest instance(s) which satisfy the constraints you selected, and will respond with the instance IDs.