Deepak Singhal
Deepak Singhal

Reputation: 10874

Stop (not terminate) an EC2 Instance when CPU drops below certain level

I have two servers (with my app on it) already running.. created from Ubuntu AMI. While using auto-scaling it starts new instances using an AMI.

Can I use auto-scaling to ONLY stop instance (not terminate; so that I don’t need an AMI when starting server in future); and later start instance (old instance which I had stopped) whenever CPU increases above x% !

If not auto-scaling; I am ok with any other solution also. I can stop the instance with Cloudwatch, but how do I restart it?

Upvotes: 3

Views: 1979

Answers (3)

Mark B
Mark B

Reputation: 200607

It can't be done with AutoScaling. You could disable AutoScaling and have CloudWatch trigger some Lambda functions to start/stop your instances.

Upvotes: 0

jarmod
jarmod

Reputation: 78663

I would think you can do this with the following:

  • CloudWatch metric: record the CPU usage
  • CloudWatch alarm: alarm when the CPU metric goes above/below some threshold
  • SNS topic: send a notification when the CloudWatch alarm is triggered
  • Lambda function: invoked by SNS to stop/start the relevant EC2 instance

See the Scaling ECS article which is similar and Invoking Lambda from SNS.

Upvotes: 7

Matt Houser
Matt Houser

Reputation: 36073

Stopping an instance cannot currently be done using AutoScaling. AutoScaling can only launch new instances and terminate instances based on CloudWatch alerts, etc.

To do what you want, you'll need to stop using AutoScaling and use another "worker" EC2 instance to replace AutoScaling. That "worker" instance need to be running 24/7 and it will monitor the CPU of your other EC2 instances and start and stop them appropriately.

Upvotes: 1

Related Questions