While I was writing this the other answers popped up - but adding detail on how to use SQS and auto-scaling.
- Set up a trigger so that a SQS message is sent when an image is uploaded to S3. https://docs.aws.amazon.com/AmazonS3/latest/userguide/NotificationHowTo.html
- Configure an auto-scaling group based on queue depth. https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-using-sqs-queue.html
- The auto-scaling group should launch the appropriate AMI (that contains your code) on a GPU-enabled instance.
- Have your code retrieve the SQS message, process the image then delete the message from the SQS queue.
- Auto-scaling will then terminate the instance if there are no more messages in the queue.
At step 5 your code could check to see if other messages have arrived and if so, process those in a loop.
The maximum number of instances in step 2 is up to you - do you need to process in parallel? Then have more than one. Do you need to process the images one at a time? Then have a maximum of 1.
Why not do all the preprocessing and the deep learning model in the Lambda function and skip EC2 entirely?
If this isn't an option for some reason - for example, if the run time is longer than the max execution limit of 15 minutes - then you could consider using Lambda to invoke an SSM Run command. The Lambda function would be invoked when an image is uploaded to the S3 bucket. The Lambda would send a run command to the EC2 instance, which could then pick up the image, perform the preprocessing and deep learning model, and return the resulting image to the other S3 bucket.
I would look at S3 Event Notification - When you upload a new image to S3 you can use this as the trigger to your Lambda function. Event notification can send events to:
Amazon Simple Notification Service (Amazon SNS) topics.
Amazon Simple Queue Service (Amazon SQS) queues.
More on S3 Event Notification . . . https://docs.aws.amazon.com/AmazonS3/latest/userguide/NotificationHowTo.html
Expanding on tedrent's answer, it's pretty easy to write a python/boto3 app to run on your EC2 which will pull S3 Event Notifications off an SQS queue and get the corresponding files from S3.