I'm working on deploying my app on an EC2 instance, and I want to save costs by automatically stopping the instance when it's not in use. The idea is to have it shut down when CPU utilization is below 5% and then start it again when an HTTPS request hits my API. I've got a solid setup, but I'm stuck on the final piece. I need a way to ensure that when the instance is off and a request comes in, it gets started, and the request is routed to the app running on the EC2 instance without the user experiencing delays. Essentially, I want some sort of 'front door' (maybe a proxy or ALB) that will trigger the instance to start and provide a smooth user experience. What are some recommended architectures or patterns to achieve this while considering latency and user experience?
4 Answers
Honestly, if you're trying to manage costs, using something like AppRunner could be a great middle ground. It simplifies things by managing the infrastructure for you and can scale to zero like Lambda, but still offers you a bit more control than completely serverless options.
If you still want to use EC2, think about implementing an ALB in front of your instance. You can set up health checks that route traffic to a Lambda function or another service when the EC2 instance is down. That way, incoming requests won't just hang while the instance is booting up. You could also show a friendly message saying something like 'We're starting up, please try again shortly.'
A good idea! You might also want to consider CloudFront, so that while your EC2 is busy starting up, any static content can be served quickly.
Consider a queuing solution like SQS with Lambda. You can modify your Lambda to write incoming requests to an SQS queue, then start your EC2 instance. Once your instance is up, it can poll the queue to process requests. This way, you don't lose any incoming requests while your instance is booting.
This could be tricky though. If you want to avoid polling, you might need to look into using SNS or EventBridge, but they might not fit perfectly with your EC2 startup situation.
You might be overthinking this a bit. If the main goal is cost savings and you want immediate response times, consider moving to a serverless architecture with Lambda instead of using EC2. Lambda automatically scales and only runs when needed, so you wouldn't have to deal with the cold start delay you're worried about with EC2.
Exactly! If you're just dealing with API requests, API Gateway with Lambda could save you a lot of hassle and costs in the long run. Plus, it's much cheaper when you're just starting out.

That's a solid suggestion! AppRunner handles everything for you and can reduce costs while also improving uptime.