mitchus
mitchus

Reputation: 4877

Suspend idle AWS instance automatically

I have to set up an AWS instance for a web application that is being used sporadically, a few hours at a time, a few times per month. The application requires a sizeable instance in terms of virtual cpus and memory, so keeping it running 24/7 would run up a steep bill, and since the time it is being used is below ~5% I am looking for a way to automatically suspend the instance if CPU utilization drops below 10% for >2 hours (for example). Also, ideally (but not strictly required) a request to the application's URL would start the instance if it is suspended.

  1. My first idea is to set up CloudWatch to record any requests to the URL, as well as the instance's CPU utilization. A Lambda function then periodically checks if the last request was over 2h ago and CPU utilization has been low for that time as well; if true then suspend the instance.
  2. The starting of the instance could be done by having a special "wakeup" URL (separate from the app's URL) which triggers a lambda function to wake the instance if asleep.

Is there a recommended or more standard way to achieve this?

Upvotes: 1

Views: 720

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 269410

You could have a script running on the instance that continually measures "usage". (The actual definition of this is up to you — it might be based on app usage, or CPU, or however you want to define it.)

Once the script determines that the instance is not being used, is can simply issue an operating system Shutdown command. If the instance Shutdown Behavior = Stop, then the instance will gracefully shutdown and stop.

You could then use your 'wake-up' process to Start the instance again.

Upvotes: 1

Emre Karataşoğlu
Emre Karataşoğlu

Reputation: 1719

You can create auto scale group that starting with 1 instance behind Elastic LB. When there is a load with given parameter ( such as cpu load, ram usage , network traffic etc ), the new instance automatically created. When the load is down, it will close the instance automatically too. There is no need to use lambda ;)

Upvotes: 0

Related Questions