Serverless AWS with EC2


Hi, I've developed a web application for image segmentation where users can upload an image and select a specific segmentation method (AI model). The image is processed on an EC2 instance, and the results are sent back to the user. This application was built using the Flask library in Python, and I've deployed the files on EC2, linking them to a domain. I opted for EC2 to utilize its GPU capabilities for model execution.

However, I'm encountering a cost-efficiency issue, as the EC2 instance needs to run continuously. I am now exploring more efficient alternatives and am considering AWS Lambda to trigger the EC2 instance only when a user uploads an image. This would allow the instance to process the image, send the results back, and then shut down, potentially reducing running costs.

Since I'm not very familiar with AWS Lambda, I'm unsure if this is the most optimized approach. I would greatly appreciate your insights on this. Additionally, if AWS Lambda is a viable solution, any references or step-by-step guides you could share would be extremely helpful.

Thank you in advance for your assistance.

asked 2 months ago96 views
2 Answers


As you can see, Lambda can be used for such use cases.
For example, you can trigger a Lambda when an image is uploaded to S3.

If you want to do it without using Lambda, I think it is better to use SQS CloudWatch metrics and configure it to start automatically with EC2 AutoScaling.
Trigger SQS and accumulate a queue when images are uploaded to S3.
I think if you set it to scale according to the number of queues, you will be able to start processing when necessary.

profile picture
answered 2 months ago
  • Thank you for the tip. I am thinking to use lambda function as I already took an online course to learn how to deploy web app with lambda. I am wondering how can I start and stop an EC2 instance with lambda when a new image is uploaded by user.


Hey, this works for starting and stopping the EC2 instances using Lambda function.

In the lambda trigger section go with S3 trigger rather than eventbridge as per your requirement.

profile picture
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions