Scenario: Users on the system are able to upload a video. The video must be run through an ML model to detect objects of interest. To run the object detection will require a high-performance GPU, but running a GPU instance 24/7 is too expensive. So I plan to deploy the backend on a small EC2 instance or Lambda and run the video processing script separately on a GPU instance on-demand.
My initial approach: Create a GPU EC2 instance (perhaps g5g.xlarge) and it will set STOPPED as the default state. The instance will implement a simple queue script, which in this case is a list of videos to process. Every time the user uploads the video, the backend will trigger the EC2 instance to start (if not already running), access it through ssh, and insert the video into the queue file.
The instance on the other hand, once started, will check for any task in the queue. And if the queue is empty, it will shut itself down.
Let me know what you think of this approach. I think this is quite simple to implement but I’m not sure if there is other additional complexity or cost that I don’t foresee.
I’m sure you’ve thought about this, but could you achieve the desired result with Amazon Rekognition and Custom Labels? It’s very use case specific, but I’d at least be testing it before I committed to training and hosting my own model and building my own scheduler for GPU.
I think Rekognition is limited to specific use case and use a pre-trained model by AWS itself. Plus, the pricing is also very expensive because you would be paying for the model inferencing + was kinesis video stream too.
Personally, I’d avoid this. It requires finding the instance, keeping secrets around, and just generally feels fragile.
Much easier to post a work item to a SQS queue, and have the instance check it on startup, and when the queue is empty, stop itself.
Further, no reason I’ve seen you can’t create a custom AMI, so you can scale out one day if you want to from one instance to many. As a bonus, you could take advantage of EC2 Auto Scaling so your instance need only concern itself with doing work, not stopping itself in when it’s done.
I have a concern with this approach. The instance will check for SQS messages, if none, it will shut down. But what if I am trying to access the server, how to make it prevent from shutting down when I am using it?
A solution that I have read is to make it have a grace period like 5 minutes before shutting down. But I think there should be other better way.
Most of the time I’ve attacked a problem like this, I don’t have the instances self-terminate, but rather use a auto-scaling setup to scale in and thus terminate the instances after x minutes of the queue being empty.
That said, whenever I do have instances self-terminate, I always have them check for a canary file first in S3 and stick around as long as the file is present so I can have them stick around if I want to debug something.