2011-06-10

Saving on EC2 costs through uptime management

For EC2 instances Amazon bills you by the hour. If you are like us, and need a few servers to be "ready" at all times - not too many, not too few - then here's a quick tip, something simple you can do to save some money on your EC2 instance costs.

For every instance, you pay at launch for the coming hour and when you cross that hour, you pay for the next one in advance. This means that stopping and starting an instance will incur an additional hour's cost - depending on when your instance was launched.

If you have several instances and don't need all of them, you can check their uptimes and see which ones are likely to cross into their next hour without being utilized until and after they do. Say, if a server has been up for 55 minutes and it's not being used at the moment, it is less likely to be used in the remaining 5 minutes than one that still has 35 minutes to go from its actual uptime hour.

But here's some actual code (in PHP, requires AWSSDKforPHP set up). Call getSortedEdgeInstanceData() to receive an array of your running instances in the order they are going to turn over into their next billing hour. I imagined these instances that are likely to turn over as "being on the edge" - hence the function name.

It's a simple idea but the costs it saves you could mount up to be significant - depending on the number of instances you manage in an on-demand manner. Hope it helps you!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//------------------------------------------------------------------------------
function sortUptimePerc($a, $b) {
//------------------------------------------------------------------------------
    if ($a["UptimePerc"] == $b["UptimePerc"]) {
        return 0;
    }
    return ($a["UptimePerc"] < $b["UptimePerc"]) ? 1 : -1;
}
 
//------------------------------------------------------------------------------
function getSortedEdgeInstanceData() {
//------------------------------------------------------------------------------
   $edge_instances = Array();
 
   $ec2 = new AmazonEC2();
   $instances = $ec2->describe_instances();
   if ($instances->isOK())
   {
      foreach($instances->body->reservationSet->children() as $reservationItem)
      {
         foreach($reservationItem->instancesSet->children() as $instanceItem)
         {
            if ($instanceItem->instanceState->name != "running")
               continue;
             
            $launchTime = $instanceItem->launchTime->to_string();
            $uptime = number_format((date("U") - date("U", strtotime($launchTime))) % 3600 / 3600, 3, ".", "");
 
            $data = Array();
            $data["InstanceId"] = $instanceItem->instanceId->to_string();
            $data["UptimePerc"] = $uptime;
            $edge_instances[] = $data;
         }
      }
   }
 
   // sort these instances by uptime, decreasing
   uasort($edge_instances, 'sortUptimePerc');
 
   return $edge_instances;
}

No comments:

Post a Comment