Is it possible to use Lambda functions along with other services to scale up and scale down(probably to 0 instances) Ec2 Deployed apps

0

Hi there, hope you are fine. Recently I came across Sagemaker Async inference API, there we can scale down even to 0 instances. What I want is that I deploy my solution to EC2 instances using FastAPI, uvicorn and Celery or Rabbit-MQ(as message broker, for queuing). Then I can scale up and scale down instances based on traffic. Also, if that's not the case, then I keep a minimal CPU instance on always and based on that I scale up and scale down GPU instances for handling requests.

Thanks , for any help.

Best Regards Muhammad Ali

2 Answers
0

Hi, I guess everything is possible in computers world and your case too. But it will take a lot of effort to do it and will not be a best practice. I have not seen somebody scale ec2's with lambda.

I guess all you need is to scale ec2s based on messages in queue (very easy with sqs) or just created a load balancer or use autoscaling group https://docs.aws.amazon.com/autoscaling/ec2/userguide/what-is-amazon-ec2-auto-scaling.html

Regards Denys

answered 2 years ago
  • Thanks Denys, for helping me out.

    As you have mentioned, it is not best practice. What do you suggest me, as in my usecase probably 80 % of the time there will be no traffic, so I want to use 0 GPU based instance, as minimal which I require will cost at least 1.2 USD/hr. And with EC2 Auto Scaling probably we cannot scale down to 0, so how should I approach it?

    Thanks Regards

0

Scaling with EC2 instances with Lambdas is a common pattern. There are a few options available to assist with this. 1/ Leverage Systems Manager to Start/Stop EC2 instances on a schedule. If you want to minimize the cost of EC2 during off hours, say at night and weekends, this is a simple solution. 2/ Cloudwatch events. You can set up CloudWatch events that trigger based upon a cron or usage of EC2. If using cron, that would trigger at a certain interval, say once a day, to run a Lambda that will shut down the EC2 instance. There are several examples you can search online that shows the code for that Lambda. If you want to have more flexibility, using CloudWatch telemetry, such as CPU utilization, to trigger a shutdown, or even start new instances. 3/ An auto-scaling group is a great way to do the same. You will set up the parameters for scale up/scale down, and the auto-scaling group will manage that for you. 4/ If you want to manage the size of all the services as an entire platform, then using containers managed by Fargate can be a solution. --You can combine these solutions to reduce consumption based upon your needs. Let's say you configure an auto-scaling group that will run during your work hours that has a minimum size of 1. At night and weekends, you have a cron that shuts down the auto-scaling group during off hours. An hour or two before work hours, a cron runs to start the auto-scaling group so you have warm instances ready to go.

AWS
Byron_G
answered 2 years 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