My Poor Man’s AWS Power Management

This post focuses more on a personal problem I experienced some time ago….and some time ago, I did it.  After privately swearing to myself that I’d never leave an EC2 instance powered on when not in use, I left one on and paid the price, literally, when my monthly AWS bill was 300% higher than normal.  To that end, I wanted to see if I could find a way to use AWS tools or services to perform a shutdown if I have an EC2 instance that is idle for two hours (based on CPU utilization) and found the following process on the AWS documentation site.

  1. First I had to figure out the idle CPU usage percentage of my EC2 instances and this is why I call this my poor man’s AWS power management technique.  I’m not necessarily guessing, but I also didn’t spend a great deal of time trying to figure it out.  Remember, I’m doing this for the personal instances that I use to edit pictures with Lightroom and Photoshop so I was satisfied with a “ballpark” number.  To ballpark this number, I simply logged into my instance, opened Lightroom, and simply watched the percentages for an hour or so.  After conducting this test, I felt comfortable saying my idle CPU usage percentage, with Lightroom running, was between 3-7%.
  2. Login to the AWS console and click CloudWatch under Management Tools

  3. On the CloudWatch dashboard, click Alarms | Create Alarm

    1-CreateAlarm

  4. On the Select Metric page, click Per-Instance Metrics under the EC2 Metrics heading

    2-perinstanceMetrics

  5. When prompted to select the Metric, find the appropriate instance and check the CPUUtilization metric. Ensure that the metric value is set to Average and then click Next.

    3-selectmetric

  6. On the Define Alarm page, under the Alarm Threshold heading, enter a unique alarm Name and Description. Additionally, specify the CPU Utilization threshold values.  In this example, the alarm threshold will be reached if CPU utilization is under 10% for 24 consecutive datapoints.  The datapoint time period is set using the Period option (highlighted below).  In this example, a datapoint is taken every 5 minutes so we’re stating that if the CPU utilization of this instances is under 10% for 2 hours, the alarm will be triggered.

    4-alarmandtime

  7. Under the Actions heading, configure a notification and email list to alert the system administrator that the EC2 instance is powering off. For Send notification to, type a name for the SNS topic.  For Email list, type a comma-separated list of email addresses to be notified when the alarm changes to the ALARM state. Each email address is sent a topic subscription confirmation email.  You must confirm the subscription before notifications can be sent to an email address. Click the +EC2 Action button to specify an EC2 instance action to take.

    5-step6

  8. On the EC2 Action section, specify the action to take. If Stop, Terminate, or Reboot this instance is selected, AWS will prompt you to create an IAM role that allows these functions to occur.  Select Create IAM role.  **This is only necessary when configuring the first alarm that stops, terminates, or reboots an instance.

    6-step7-alarmaction

  9. With the alarm successfully defined, click Create Alarm.
  10. Open your email application and confirm the SNS topic subscription by clicking Confirm subscription 
  11. Once the alarm threshold is crossed the notification email, such as the example shown below, is sent to those in the email list who have confirmed their subscription.

    7-statusemail

Not satisfied with the email, I logged into the AWS console to ensure my machine did power down only to find that the alarm worked perfectly and performed as configured.  Again, I don’t think I’d do this for production workloads as enterprises likely have a production-caliber tool to help them perform power management tasks.  But if you have a personal lab of EC2 instances in AWS and you want to make sure they power off if not in use, you may find this technique useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s