Wednesday, June 10, 2015

Programming monogamy

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

No comments:

A little about Me

My photo
My name is Mitch Beaumont and I've been a technology professional since 1999. I began my career working as a desk-side support engineer for a medical devices company in a small town in the middle of England (Ashby De La Zouch). I then joined IBM Global Services where I began specialising in customer projects which were based on and around Citrix technologies. Following a couple of very enjoyable years with IBM I relocated to London to work as a system operations engineer for a large law firm where I responsible for the day to day operations and development of the firms global Citrix infrastructure. In 2006 I was offered a position in Sydney, Australia. Since then I've had the privilege of working for and with a number of companies in various technology roles including as a Solutions Architect and Technical team leader.