Score:0

AWS EC2 instance: can an alarm hibernate an instance instead of shutting it down?

cn flag

I have an EC2 instance of type c5, which is hibernate-capable, and I can hibernate it manually, for example from the AWS EC2 console.

This is for a spot-instance desktop machine, and I have an alarm which shuts it down after a certain amount of low CPU activity. I wish that the alarm could hibernate, but I can't see this option. I am missing something?

Tim avatar
gp flag
Tim
I've never tried to do this, but you could look at triggering a lambda function then doing it in lambda.
cn flag
thanks, good idea. I have a script in the guest called 'hibernate-me' which uses aws cls to hibernate itself. I am trying to get this to run when the session locks due to timeout .. so much fun.
Tim avatar
gp flag
Tim
A cloudwatch events alarm for low CPU calling a lambda function is probably the simplest way to do this. If you search the internet you might find a script that does this for you, but if not it wouldn't be that difficult to write if you know how to code.
Score:0
de flag

You could write a script to run from the cron ( I assume your guest OS is Linux) periodically to check CPU usage, for example running and parsing the command line: grep 'cpu' /proc/stat.

When the CPU usage meets your criteria, you will run the command (I hope you can run the AWS CLI from your instance):

aws ec2 stop-instances --instance-ids --hibernate

The instance-id can be found inside of the guest OS by running this command:

wget -q -O - http://169.254.169.254/latest/meta-data/instance-id

or run the command

ec2-metadata -i

You could also try to look at the AWS System Manager.

https://docs.aws.amazon.com/systems-manager/latest/userguide/automation-document-script-commandline.html

However, the latter solution is quite complex.

cn flag
thanks, I already have a "hibernate_me" aws cli script which hibernates the instance from within the Linux guest. The best thing would be to trigger this on the dbus message for when the session locks due to inactivity, I think, but dbus is very mysterious to me.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.