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”).

gem install aws-sdk

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:

require 'aws-sdk'
require 'rubygems'
Aws.config.update({
region: 'us-east-1',
credentials: Aws::Credentials.new('aws_access_key_id', 'aws_secret_access_key')
})

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.

ec2 = Aws::EC2::Client.new
puts ec2

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:

puts ec2.operation_names

operation_names

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

eresp= ec2.describe_instances
puts eresp

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:

puts eresp.successful?

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.

puts eresp.reservations

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.

eresp.reservations.first
eresp.reservations[0]

Fetching the data:
Fetching reservation_id from the reservation:

puts eresp[:reservations][0][:reservation_id]

reservation_id

Two different ways to obtain the tag value:

puts eresp.reservations.first.instances[0].tags[0].value
puts eresp[:reservations][0][:instances][0][:tags][0][:value]

tags-value

Fetching Data using for-each loop:

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

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.

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

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 comment -