Utilizing AWS Ruby SDK to get AWS EC2 details in a CSV file

20 / Jun / 2016 by Vaibhav Gulati 0 comments

Over the years, Amazon has been integrating its web services with various programming languages to make it easier for developers to utilize various AWS resources effectively. It has made specific tools for popular programming languages like Ruby, Python, Java, .NET etc.

Talking about Ruby, it is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro “Matz” Matsumoto in Japan.

AWS(Amazon Web Services) SDK for Ruby gives APIs for accessing and building applications over the services provided by AWS. With their simple to use SDK, one can perform various operations on AWS resources.
Screenshot from 2016-06-20 10:48:49

Use case

This blog shows how to use AWS SDK using Ruby to get AWS EC2 details, which can help to automate the creation of an inventory file and help save time to a great extent. This way, EC2 details will be fetched and stored in a CSV file which can be easily interpreted.

Requirements

  • Ruby(v2.2+)
  • AWS Account (Access Key Id, Secret Key)
  • Version2 of AWS SDK

 

Steps To Follow

Download gems:
RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a “gem”).

[sourcecode language=”ruby”]gem install aws-sdk[/sourcecode]

Create a “<file_name>.rb” file for writing ruby script and to compile and run use “ruby <file_name>.rb”

Making a Connection with AWS Account:

[sourcecode language=”ruby”]require ‘aws-sdk’
require ‘rubygems’
Aws.config.update({
region: ‘us-east-1’,
credentials: Aws::Credentials.new(‘aws_access_key_id’, ‘aws_secret_access_key’)
})[/sourcecode]

Creating an EC2 object:
Creating an EC2 Object by using the Aws object after the connection.
This object will be used later to fetch EC2 details.

[sourcecode language=”ruby”]ec2 = Aws::EC2::Client.new
puts ec2[/sourcecode]

client
An EC2 client response is received, this means object is created successfully.

Methods allowed on ec2 object:
This gives a list of operations that can be performed on the EC2 object:

[sourcecode language=”ruby”]puts ec2.operation_names[/sourcecode]

operation_names

Using method describe_instances:
Using describe_instances method to get various details of the EC2 object.

[sourcecode language=”ruby”]eresp= ec2.describe_instances
puts eresp[/sourcecode]

seahorse_client
The above operation returned a Seahorse Client Response object which has two methods:
1.) reservations
2.) next_token

Check if the operation was successful by using the method “successful?” on the response:

[sourcecode language=”ruby”]puts eresp.successful?[/sourcecode]

eresp_success

Using “reservations” method on the received response:
Each instance in the AWS Account gets its own Reservation structure which contains all the details of the instance.

[sourcecode language=”ruby”]puts eresp.reservations[/sourcecode]

It returns an array of ruby structures.array of structures of ruby

Iterating over the array and fetching data from the structure:
The first element of the array can be iterated/traversed by either using any of the two ways discussed below or by applying for-each loop discussed later.

[sourcecode language=”ruby”]eresp.reservations.first
eresp.reservations[0][/sourcecode]

Fetching the data:
Fetching reservation_id from the reservation:

[sourcecode language=”ruby”]puts eresp[:reservations][0][:reservation_id][/sourcecode]

reservation_id

Two different ways to obtain the tag value:

[sourcecode language=”ruby”]puts eresp.reservations.first.instances[0].tags[0].value
puts eresp[:reservations][0][:instances][0][:tags][0][:value][/sourcecode]

tags-value

Fetching Data using for-each loop:

[sourcecode language=”ruby”]eresp[:reservations].each do |reservation|
reservation[:instances].each do |instance|
instance[:tags].each do |tag|
puts tag[:value]
end
puts instance[:private_ip_address]
puts instance[:image_id]
puts instance[:state][:name]
puts instance[:instance_type]
end
end[/sourcecode]

data_fetched

Putting data in a CSV file:
Putting all the fetched data in a CSV file (test.csv) using File class and using its new() method, ‘a+’ to append data to the file.

[sourcecode language=”ruby”]eresp[:reservations].each do |reservation|
reservation[:instances].each do |instance|
instance[:tags].each do |tag|
file=File.new(‘test.csv’,’a+’)
file.write tag[:value] + "\t"
file.write instance[:private_ip_address] + "\t"
file.write instance[:image_id] + "\t"
file.write instance[:state][:name] + "\t"
file.write instance[:instance_type] + "\t"
file.write instance[:architecture] + "\t"
file.write instance[:hypervisor] + "\t"
file.write instance[:client_token] + "\t"
file.write instance[:key_name] + "\t"
file.write instance[:root_device_type] + "\t"
file.write instance[:state][:name] + "\t"
file.write instance[:virtualization_type] + "\t"
file.puts
end
end
end[/sourcecode]

data_in_csv

Hope this blog gave you a kick-start on using AWS Ruby SDK. I will be coming up with more such blogs soon.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *