Programming monogamy

Jun 10, 2015 14:27 · 559 words · 3 minute read ruby php aws

I decided it was high time I bought an end to my exclusive relationship with PHP and started playing the field a little.

I’m not suggesting that there is anything wrong with PHP. Exactly the opposite in fact, not coming from a development background, I’ve been able to achieve some fantastic things with PHP. My journey from “total PHP newb” to “not so much of a PHP newb” has introduced me to a new whole new world and some very interesting concepts. Starting to understand application development and some of the application programming principals has helped improve my understanding of software development within my company. It has enabled me to have more constructive conversations with developers across my organisation, this was especially relevant when it came to re-factoring some of our software solutions for AWS.

That aside, I recently met this little beauty who goes by the name of Ruby. Why Ruby and not Python or Node or some other scripting language. The main motivator was Rails. Rails is a web application framework that I’ve heard lots about and am keen to explore.

A lot of the PHP work I’ve done has been around building web applications and browser based consoles for managing environments and services within our environment. I never got really stuck into a framework for developing applications in PHP, I normally handcraft everything with a simple MVC structure, like the one below.

This practice got hammered home thanks to the cover-to-cover reading of “PHP for Absolute Beginners”, which I’d highly recommend to anyone looking to get started with PHP or web application development in general.

Anyway, I digress, since Ruby and Rails “appear” to go hand-in-hand, I decided to start learning some Ruby.

I wanted to start, as I did with PHP, with something really simple. Since most of what I do these days resolves around AWS, pulling back a list of EC2 instances and dumping them to the console seemed like a perfect place to start.

In my first little script below, I’ve created an empty hash, built a function which uses the AWS SDK for ruby to return a list of instances and populate the hash with a subset of the information returned.

It then iterates through the hash, using the awesome .each method and spits out a “nicely” formatted report to the console.

Pretty basic, but it gave me the chance to get an grasp on some basic Ruby concepts like symbols and the awesome .each method.

These simple scripts inevitably form the building blocks for larger and more complex solutions, so sit tight and lets see where Ruby and I go from here.

 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
#!/usr/bin/ruby   
  
require "aws-sdk"  
  
$instance\_hash \= Hash.new('Nothing New')  
  
################################  
\# => Function: get\_ec2\_instances  
\# => returns all running EC2 Instances for my account.  
###############################  
  
def get\_ec2\_instances  
 ec2 \= Aws::EC2::Client.new(region: 'ap-southeast-2')  
  
 resp \= ec2.describe\_instances()  
 resp\[:reservations\].each do | reservations |  
  reservations\[:instances\].each do | instances |  
   $instance\_hash\[instances\[:instance\_id\]\] \=   
    {  
    "accountId" \=> reservations\[:owner\_id\],  
    "state" \=> instances\[:state\]\[:name\],   
    "privateIp" \=> instances\[:private\_ip\_address\]  
    }  
  end  
 end  
end  
  
get\_ec2\_instances  
  
$instance\_hash.each do |key,value|  
 puts "Intstance Id: #{key}"  
 value.each do |k,v|  
  puts "#{k} : #{v}"  
 end  
 puts "-" \* 25  
end