Hi everyone,
I’m trying to deploy an application on a single EC2 instance and reduce costs by stopping the instance when it’s idle, then automatically starting it again when someone calls my API over HTTPS. I’ve made some progress, but I’m stuck on the last part and would appreciate advice on the right architecture or patterns.
*What I want to achieve
*
Automatically stop the EC2 instance when it’s idle (for example, CPU utilization < 5%).
When an incoming HTTPS request hits my API:
The EC2 instance should be started automatically if it’s stopped.
The request should eventually be served by the application running on that EC2 instance.
*What I’ve already done
*
I successfully auto-stop the EC2 instance using a CloudWatch alarm that triggers StopInstances.
I created a Lambda function with the required IAM permissions to start the EC2 instance.
I exposed that Lambda through API Gateway (HTTP API → Lambda → Start EC2), and this part works: calling the API starts the instance.
*The problem
*
The API Gateway endpoint is not the EC2 endpoint. When the instance is stopped:
The HTTPS request triggers the Lambda and starts the EC2 instance.
However, the original request is never forwarded to the application once the instance finishes booting.
In other words, the requester’s request is effectively lost because the EC2 instance was off at the time the request arrived.
*My question
*
Is there a practical way to keep a “front door” (proxy, ALB, API Gateway, etc.) in front of the EC2 instance such that:
Incoming HTTPS requests trigger the instance to start if it’s stopped, and
The request is either:
Forwarded to the app once the instance is ready, or
Responded to with a friendly message like “Service is starting, please retry in X seconds”
*I’m considering options like:
*
Application Load Balancer
API Gateway + Lambda
A reverse proxy setup
…but I’m not sure what the best pattern is, or what the tradeoffs are in terms of latency, complexity, and user experience.
Any recommended architectures, existing AWS patterns, or implementation tips would be greatly appreciated. Bonus points if you can comment on cold-start latency and UX considerations.
Thanks!
Top comments (0)