blog

Checking Amazon EC2 Reserved Instances

March 30th, 2014

If you are using Amazon EC2 servers, you know that it is a lot cheaper if you purchase a reserved instance rather than paying hourly for servers that you intend to run continuously. On my projects, we typically have quite a few servers where we purchase “heavy utilization” reserved instances because we intend to run them day-in and day-out for many months. However, the Amazon Web Services management console makes it really difficult to tell if your currently running EC2 instances match up to reserved instances. And it’s also difficult to know when a reserved instance is about to expire so that you can purchase a new one.

On top of that, when you create a new account with Amazon, you get to run a t1.micro instance free for a year. They call this their “free tier.” But it is very difficult to tell when your free period is about to expire so that you can then purchase a reserved instance instead. So I wrote some PHP code to rectify all of these issues.

My code can output either HTML for a web browser, or it can send an HTML email to someone. I have it set up to send me an email every couple of weeks by using a cron job to check my two Amazon accounts. I also have it set up to show the results in a web page on an administrative server for the printui.com startup. The output looks something like this…

         Summary for Account

All running instances have a matching reserved instance

Details for Account

Servers
Res Name Zone Type Instance
X server1.com us-east-1a c1.xlarge i-b32cecd9
X server2.com us-east-1a t1.micro i-12ff0a40
X server3.com us-east-1a t1.micro i-3456dcbe

 

Reserved Instances
Used Zone Type
X us-east-1a t1.micro
X us-east-1a c1.xlarge
X free-tier t1.micro

If an instance will expire within two weeks, the summary will tell you exactly when it will expire. If a reserved instance has already expired, the results will show you which EC2 instance is affected.

You can download a ZIP file of the complete the source code from here:

        checkreserved.zip

To use it yourself, edit the config.php file and set the various values. The define’s that start with “G_smtp” are used to send the emails via SMTP. Note that you must have the PEAR Mail and Mail_Mime extensions installed for this to work. The $account array defines your Amazon accounts, including the date when the free tier started (i.e. the date you opened your Amazon account). This code has been tested with PHP 5.3.

Posted in Amazon Web Services, Cloud Computing | No Comments »

Programmers on a boat?

June 22nd, 2012

Steve Mezak at Accelerance has written an interesting article about a proposal by a company called Blueseed to put a bunch of programmers on a boat right off the shore of Silicon Valley. Is it preposterous? You decide.

http://bit.ly/MdvxJT

Posted in Software Development | No Comments »

Get an IP range for a domain using PHP

March 8th, 2012

One of my server hosting providers tells me that outgoing traffic from their data center can have any IP address within a /22 range.  So starting with the domain, I wanted to create a list of all of the IP addresses within that range to put in a whitelist.  This was actually a bit trickier than it sounds.  I ended up using a lot of binary operators and bitwise masking.  Since PHP doesn’t support unsigned int’s, I had to do a bit of extra masking to account for a possible sign bit.  So here’s my code (below).  I execute an nslookup and then parse the results for the IP address.  I then apply the range mask and create an array of IP addresses.  This code works for both Windows and Linux.


<?php
define ('CRLF', "\r\n");
setlocale(LC_ALL, 'us');
date_default_timezone_set("UTC");

header('Content-Type: text/plain');

$ips = getip('pmierau.com/30');
foreach ($ips as $ip)
  print $ip.CRLF;

exit;

/*
 * Get the array of IP addresses for a specific domain
 * 
 * If a range is specified for the domain (i.e. xyz.com/24), the returned array returns
 * IP aaddresses for the entire range (i.e. 256 addresses for a /24 range)
 */
function getip($url)
{
  $ips = array();
  
  $urlparts = explode('/', $url);
  if (isset($urlparts[1]))
  {
    $range = (int)$urlparts[1];
    $url = $urlparts[0];
  }
  else
  {
    $range = 0;
  }
  
  $cmd = 'nslookup '.$url;
  $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
  $process = proc_open($cmd, $descriptorspec, $pipes);
  if (is_resource($process))
  {
    $results = stream_get_contents($pipes[1]);
    $results .= stream_get_contents($pipes[2]);

    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);

    $return_value = proc_close($process);
    
    // Parse the font information
    $lines = explode("\n", $results);
    $answer = false;
    foreach ($lines as $line)
    {
      $line = trim($line);
      
      if (false !== strpos($line, $url))
      {
        $answer = true;
      }
      elseif ($answer && ((false !== strpos($line, 'Address:')) || (false !== strpos($line, 'Addresses:'))))
      {
        $parts = explode(':', $line);
        $ip = trim($parts[1]);
        
        if ($range == 0)
        {
          $ips[] = $ip;
        }
        else
        {
          $binaryip = binaryip($ip);
          $mask = (0xffffffff << (32 - $range)) & 0xffffffff;
          $startbinaryip = $binaryip & $mask;
          $n = (~$mask) + 1;
          
          for ($ip = $startbinaryip, $i = $n; $i != 0; $ip++, $i--)
            $ips[] = ipv4ip($ip);
        }
        
        return $ips;
      }
    }
  }

  $ips[] = '?.?.?.?';
  return $ips;
}

/*
 * Convert an IPV4 address to it's binary equivalent
 */
function binaryip($ip)
{
  $ipparts = explode('.', $ip);
  $binaryip = 0;
  foreach ($ipparts as $ippart)
  {
    $binaryip = $binaryip << 8;
    $binaryip |= (int)$ippart;
  }
  
  return $binaryip;
}

/*
 * Convert a binary IP address to traditional IPV4 format
 */
function ipv4ip($binaryip)
{
  $ip1 = (($binaryip & 0xff000000) >> 24) & 0x000000ff;
  $ip2 = ($binaryip & 0x00ff0000) >> 16;
  $ip3 = ($binaryip & 0x0000ff00) >> 8;
  $ip4 = ($binaryip & 0x000000ff);
  
  return $ip1.'.'.$ip2.'.'.$ip3.'.'.$ip4;
}

?>

Posted in Software Development | No Comments »

Why I Choose to Use PHP

June 18th, 2011

I have standardized on using PHP for all of our server projects.  My criteria for choosing a language were:

  • It must be used by a large number of programmers.  If I hire someone, they need to hit the ground running instead of going through a learning curve.
  • There must be a large number of open source and paid software packages available in the language.
  • It must work on both Linux and on Windows Server.
  • It must be straightforward to deploy onto new servers.
  • Sample code must be widely available on the Internet.

There are lots of server programming languages, so let me go through several of them so I can share my thinking related to my criteria above.

C# is an excellent language, and I’ve used it a lot for code that runs on Windows.  However, on servers, it’s pretty much tied to ASP.NET, which is tied to Windows Server.  Most of our servers are Linux, so C# doesn’t really work.

Ruby has been hot for a couple of years now.  But there just seems to be a little too much “magic” associated with Ruby for my tastes, whether it’s the naming conventions in Rails or the things you need to do to deploy it in a scalable way.  There are some software packages written in Ruby, but nowhere near as many as PHP.  And fewer programmers know Ruby.

Java would probably be my second choice.  But I’ve always disliked how “heavy” Java feels to me.  There are lots of software packages available, though, and lots of programmers know Java.

And there are many other choices such as Perl, Python, C++, etc.  There just aren’t enough people that know them, and there aren’t enough software packages available.

PHP, on the other hand, meets all of my criteria.  Lots of programmers know it.  There are lots of software packages.  And there is tons of sample code on the Internet, so just about every conceivable issue has been covered.  On top of that, I’ve been impressed by how rich the language is.  Although it’s not perfect, it has served our needs very well so far.

Posted in Software Development | No Comments »

What skills does a VP of Engineering need?

September 1st, 2010

I had an interesting conversation with a recruiter yesterday who was looking for a VP of Engineering for a startup gaming company.  He started off by saying he was looking for someone with strong Flash programming skills.  Part of our conversation revolved around whether it really matters whether a VPE needs skills in a specific programming language.  (By the way, “Flash” programming is actually FLEX and ActionScript programming.)  I contend that the programming language doesn’t really matter.  It takes the same management skills for C++ programmers as it does for Flash programmers.  What’s really important are architecture and design skills.  A VPE needs to be able to intelligently review and question a software design to make sure that it meets the requirements and isn’t going to box everyone in a corner due to some flaw.  If there are bugs or issues specific to the programming language, the VPE’s job should be to know who on the team has the right expertise to address them, not to fix them himself.

I myself have been in this position more than once.  For example, one project used Java as the programming language.  Although I am no expert on Java, I was able to successfully manage the project because I did understand the architecture and design, and I had folks on my team that I could rely on to handle the nitty-gritty Java issues that did arise.

Posted in Software Development | No Comments »

Pros and cons of cloud computing versus dedicated servers

June 21st, 2010

I’m a big believer in cloud computing.  I especially like the idea of using third-party cloud computing on a “utility” basis (e.g. Amazon web services).  Why?  It’s so easy to get started and it’s so easy to scale according to load—assuming you’ve architected things properly.  So why, then, should you ever consider using dedicated physical servers in a data center?

First of all, you can get higher performance.  You’re not just deploying servers in the data center, you are also deploying networking gear like firewalls, load balancers, and switches.  So you have full control over everything.  Because the servers are in the same physical rack as the networking gear, latency is almost non-existent.  In the very common case where you use web services or database servers, this can give you a small but noticeable boost in performance.  You can choose your networking gear to have very high performance with accelerators built into the hardware.  In addition, if you don’t virtualize your servers, you also avoid the small amount of overhead due to the virtual OS (e.g. Xen, VMware, etc.).

Second, it can be cheaper.  But you have to have a fairly large number of servers before this is the case due to amortizing the cost of your networking gear across the servers.  And I’m also assuming you can depreciate your hardware over at least three years.  It also helps if you use inexpensive white-box servers to keep the costs down.

Third, you can be fully Level 1 PCI compliant for ecommerce sites.  Level 1 requires physical access to the servers for the PCI audit, but Amazon, for example, won’t allow for physical access to their cloud computing servers.  (Lower compliance levels are possible, though.)

Fourth, you have the option of doing fancier networking.  For example, you could easily route your traffic to a “site down” web page if all of your web servers went off line for some reason.  You could also inspect the incoming URL and send some traffic to one pool of servers but other traffic to a different pool.  You can terminate SSL in one place instead of on every server, and also take advantage of high-performance SSL hardware.  And much more…

Fifth, you have full control over who has access to the data on the servers.  This might be important for highly sensitive data, such as a corporate database, where you want to maintain the highest amount of control due to SOX compliance.

Sixth, you could also implement a hybrid solution whereby you use physical servers for some things, but cloud computing for other things.  For example, let’s say that your site does a lot of heavy processing of photos.  Do the photo processing in a third-party cloud using web services, but do everything else on your physical servers.  In another example, you could use cloud computing for overflow processing.  Your day-to-day processing could be done on your physical servers, but if the load gets too high you could add servers in the cloud on the fly.

Of course, there are also many disadvantages of using physical servers.  Perhaps foremost among them, you have the expertise to be able to set up all the servers or networking gear.  (Although you could use some one-time consulting to do the setup and then do the relatively straightforward day-to-day activities yourself.)  You also have to sometimes travel to the data center to do setup and maintenance work.  Unless the data center is close by, this could get expensive.

Posted in Data Centers | No Comments »

What is different about a Web 2.0 project?

June 14th, 2010

For a while now, you’ve probably heard the term “Web 2.0” loosely bandied about.  But what does it really mean?  I don’t think there’s any hard and fast definition.  It’s more of a broad philosophy.  But I like to use the term to distinguish my projects from other types of projects, especially “enterprise” projects.

As the name implies, “Web 2.0” means that you have a web site of some sort.  It might be an online store.  Or it might be web services.  There are lots of possibilities.  But in general, you are evolving continuously based on user needs.  From a project perspective, this means that you are using an agile development methodology and frequent software releases.  You tend to use open source software whenever possible to keep licensing costs to a minimum.  It also means that the software architecture must be very modular so that the number of servers for one functional module can scale independently from those needed for some other module.  For example, one functional module could be your front-end web servers, while another module could be authentication web services.

“Web 2.0” also has implications for the data center hardware.  Again, you need to be evolving and adjusting very quickly.  The ideal way to do this is by using pure cloud computing.  Cloud computing lets you easily create or remove virtual servers as your load changes over time.  However, you can also evolve quickly even if you’re using fixed servers in your own data center.  (More later on the pros and cons of cloud computing versus fixed servers.)  The trick is to use commodity hardware so that you can buy and install more servers and infrastructure gear very quickly because they are readily available.  For example, you could use cheap, off-the-shelf Dell servers that you can order via Dell’s web site.

Let’s contrast this with the typical enterprise project.  In many cases, the project needs to be planned months in advance.  The servers are usually expensive, high-end servers that require longer lead times.  Cloud computing is generally frowned upon.  The software development is usually a one-shot project for a fixed duration and there are usually very high licensing costs.

Posted in Data Centers, Software Development | No Comments »

REST versus SOAP for web service APIs

May 31st, 2010

When creating web service APIs, one of the first things you need to decide is what interface method should be used.  Two very common methods are REST over HTTP and SOAP over HTTP.  HTTPS could also be used for either for highest security.  (There are other methods such as JSON or XML-RPC, but I contend that they aren’t as widely supported as REST or SOAP.)

So which one to pick?  After using both methods, I tend to lean towards REST.  I like the simplicity of REST, both from the point of view of defining the API, but also because it can be easier to debug.  REST uses the HTTP verbs of GET, PUT, POST, and DELETE as the basis for implementing an API.  For example, to retrieve a file from a server, you would perform an HTTP GET after specifying the filename and path in the URL.  To me, this is easy to understand and very versatile.  It’s also pretty easy to debug by just using an “HTTP sniffer” like Wireshark or Charles to watch the traffic going back and forth.

SOAP, on the other hand, is more about providing strong typing for variables and for support of more elaborate structures.  By using a WSDL, it can be very easy to provide accurate documentation of the APIs to a programmer implementing a client.  And if both the client and server are implemented in a .NET programming language, it can be very easy to implement sophisticated APIs.  On the other hand, if you want your API to be widely used by lots of clients with disparate programming languages, many don’t provide nice support for SOAP and WSDLs.  And SOAP can be tricky to use and to debug because it requires more elaborate interactions between the caller and the server.  You could use an HTTP sniffer for debugging, but it’s harder to tell what’s going on.

So overall, I prefer REST.  It just seems cleaner to me.

Posted in Software Development | No Comments »

Using SOA principles for small sites

May 24th, 2010

If you read about Service Oriented Architecture (SOA) for server-based applications, you’ll find that SOA means different things to different people, especially for enterprise environments.  In fact, there are some specific acronyms and methodologies that can make SOA seem really complex.  But the basic principles of SOA can be used by anyone to improve their site, even small ones.

Think about it this way…  Divide your application into functional modules, with each module having a set of web service APIs.  You even probably know intuitively how to divide things up.  For example, it’s very common for end users to authenticate by logging in.  So authentication could be one of your functional modules, and your front-end web servers could call it via web service APIs.  Or you may have to scale JPEG images to create thumbnails, so that could be another functional module.

The ideal way to implement this basic SOA scheme is to put each functional module on one or more servers dedicated just that one function.  And when your site gets bigger, that’s exactly what you should do.  But if you have a really small site, you’ll want to save money by putting more than one functional module on the same server.  The trick is to minimize one module from interfering with the other.  For example, you probably wouldn’t want to put authentication on the same server as thumbnail scaling because the scaling function requires a lot of heavy-duty CPU usage.  It could cause authentication requests to slow down to a crawl.

There are several benefits from using this basic SOA approach.  First, you reduce risk by only deploying new code to the servers where you’ve made changes.  All the other servers remain unchanged.  If something starts going wrong right after the deployment, it’s much easier to pinpoint it to the isolated change you’ve just made.

Second, your site is much easier to scale.  By monitoring traffic to each functional cluster of servers, you know when you need to add servers.  On the other hand, if many functions are hosted on the servers, it’s much harder to tell which one is causing the load.

Third, by putting different functionality on different servers, you are forced to make sure the APIs are very clean.  There’s no temptation to cut corners and make calls directly between modules located on the same server, so you avoid “spaghetti code” that is difficult to break apart later when you need to scale.

Posted in Data Centers, Software Development | No Comments »

Why developers should be first response for escalations

May 17th, 2010

You’ve put lots of blood, sweat, and tears into developing a great online product, and you’re ready to launch it.  But how are you going to handle the inevitable problems and issues that that cause down time once you’re open to the public?  You realize that you need to create an escalation plan—one that specifies who should be responding first.  Should it be the Network Operations Center (NOC) at your data center?  Or should it be your developers?  Someone else?  In my experience, more than 90% of the time, the problems that arise are due to software bugs or configuration issues, and not hardware or networking.  That argues for your developers to be the first responders.  Otherwise, you’re wasting precious time while the NOC finds one of their technicians to try to troubleshoot the issue, only to discover they can’t do anything about it.

On the other hand, a developer usually can determine very quickly if an issue is a hardware or networking problem and call the NOC.  (Most networking problems that take the site down are catastrophic.)  The developers are also going to be most familiar with how the latest code is supposed to work, whereas the data center folks may be pretty unfamiliar with your site, especially since you are just one of many clients that they support.

When you write your escalation plan with developers as the first responders, you may get some grumbling from them.  After all, who wants to get awakened in the middle of the night?  But think of this as an incentive.  The more solid the code, the less likely they’ll get called.  And if they do get called in the middle of the night, you’ll be darn sure that they will put in a really solid fix for the issue.

Posted in Data Centers, Software Development | No Comments »

« Older Entries