{"id":35257,"date":"2016-06-20T11:11:16","date_gmt":"2016-06-20T05:41:16","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=35257"},"modified":"2016-06-20T12:34:35","modified_gmt":"2016-06-20T07:04:35","slug":"aws-sdk-ruby-csv-ec2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aws-sdk-ruby-csv-ec2\/","title":{"rendered":"Utilizing AWS Ruby SDK to get AWS EC2 details in a CSV file"},"content":{"rendered":"<p>Over the years, Amazon has been integrating its web services with various programming languages to make it <a title=\"aws devops engineer\" href=\"http:\/\/www.tothenew.com\/devops-automation-consulting\" target=\"_blank\">easier for developers<\/a> to utilize\u00a0various AWS resources effectively. It has made specific tools for popular programming languages like Ruby, Python, Java, .NET etc.<\/p>\n<p>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 &#8220;Matz&#8221; Matsumoto in Japan.<\/p>\n<p><strong>AWS(Amazon Web Services) SDK<\/strong> for Ruby gives APIs for accessing and building applications over the services provided by AWS. With their\u00a0simple to use SDK, one can perform various operations on AWS resources.<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35868\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Screenshot-from-2016-06-20-104849.png\" alt=\"Screenshot from 2016-06-20 10:48:49\" width=\"626\" height=\"202\" \/><\/p>\n<h2>Use case<\/h2>\n<p>This blog shows how to use AWS SDK using Ruby to get AWS EC2 details, which can <a title=\"aws automation\" href=\"http:\/\/www.tothenew.com\/devops-aws\">help to automate<\/a> the creation\u00a0of 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.<\/p>\n<h2>Requirements<\/h2>\n<ul>\n<li>Ruby(v2.2+)<\/li>\n<li>AWS Account (Access Key Id, Secret Key)<\/li>\n<li>Version2 of AWS SDK<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><strong>Steps To Follow<\/strong><\/h2>\n<p><strong>Download gems<\/strong>:<br \/>\nRubyGems 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 &#8220;gem&#8221;).<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]gem install aws-sdk[\/sourcecode]<\/p>\n<p>Create a &#8220;&lt;file_name&gt;.rb&#8221; file for writing ruby script and to compile and run use &#8220;ruby &lt;file_name&gt;.rb&#8221;<\/p>\n<p><strong>Making a Connection with AWS Account:<\/strong><\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]require &#8216;aws-sdk&#8217;<br \/>\nrequire &#8216;rubygems&#8217;<br \/>\nAws.config.update({<br \/>\nregion: &#8216;us-east-1&#8217;,<br \/>\ncredentials: Aws::Credentials.new(&#8216;aws_access_key_id&#8217;, &#8216;aws_secret_access_key&#8217;)<br \/>\n})[\/sourcecode]<\/p>\n<p><strong>Creating an EC2 object:<\/strong><br \/>\nCreating an EC2 Object by using the Aws object after the connection.<br \/>\nThis object will be used later to fetch EC2 details.<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]ec2 = Aws::EC2::Client.new<br \/>\nputs ec2[\/sourcecode]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35333\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/client.png\" alt=\"client\" width=\"336\" height=\"43\" \/><br \/>\nAn EC2 client response is received, this means object is created successfully.<\/p>\n<p><strong>Methods allowed on ec2 object:<\/strong><br \/>\nThis gives a list of operations that can be performed on the EC2 object:<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]puts ec2.operation_names[\/sourcecode]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35334\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/operation_name.png\" alt=\"operation_names\" width=\"343\" height=\"389\" \/><\/p>\n<p><strong>Using method describe_instances:<\/strong><br \/>\nUsing describe_instances method to get various details of the EC2 object.<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]eresp= ec2.describe_instances<br \/>\nputs eresp[\/sourcecode]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35337\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/seahorse_client.png\" alt=\"seahorse_client\" width=\"422\" height=\"41\" \/><br \/>\nThe above operation returned a Seahorse Client Response object which has two methods:<br \/>\n1.) reservations<br \/>\n2.) next_token<\/p>\n<p><strong>Check if the operation was successful by using the method \u201csuccessful?\u201d on the response:<\/strong><\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]puts eresp.successful?[\/sourcecode]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35340\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/eresp_success.png\" alt=\"eresp_success\" width=\"265\" height=\"37\" \/><\/p>\n<p><strong>Using &#8220;reservations&#8221; method on the received response:<\/strong><br \/>\nEach instance in the AWS Account gets its own Reservation structure which contains all the details of the instance.<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]puts eresp.reservations[\/sourcecode]<\/p>\n<p>It returns an array of ruby structures.<img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-35401\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/rsz_array_of_structures_edited.png\" alt=\"array of structures of ruby\" width=\"651\" height=\"210\" \/><\/p>\n<p><strong>Iterating over the array and fetching data from the structure:<\/strong><br \/>\nThe 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.<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]eresp.reservations.first<br \/>\neresp.reservations[0][\/sourcecode]<\/p>\n<p><strong>Fetching the data:<\/strong><br \/>\nFetching reservation_id from the reservation:<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]puts eresp[:reservations][0][:reservation_id][\/sourcecode]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35342\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/reservation_id.png\" alt=\"reservation_id\" width=\"268\" height=\"40\" \/><\/p>\n<p>Two different ways to obtain the tag value:<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]puts eresp.reservations.first.instances[0].tags[0].value<br \/>\nputs eresp[:reservations][0][:instances][0][:tags][0][:value][\/sourcecode]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35344\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/tags-value.png\" alt=\"tags-value\" width=\"266\" height=\"56\" \/><\/p>\n<p><strong>Fetching Data using for-each loop:<\/strong><\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]eresp[:reservations].each do |reservation|<br \/>\nreservation[:instances].each do |instance|<br \/>\ninstance[:tags].each do |tag|<br \/>\nputs tag[:value]<br \/>\nend<br \/>\nputs instance[:private_ip_address]<br \/>\nputs instance[:image_id]<br \/>\nputs instance[:state][:name]<br \/>\nputs instance[:instance_type]<br \/>\nend<br \/>\nend[\/sourcecode]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35345\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/data_fetched.png\" alt=\"data_fetched\" width=\"264\" height=\"109\" \/><\/p>\n<p><strong>Putting data in a CSV file:<\/strong><br \/>\nPutting all the fetched data in a CSV file (test.csv) using File class and using its new() method, &#8216;a+&#8217; to append data to the file.<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]eresp[:reservations].each do |reservation|<br \/>\nreservation[:instances].each do |instance|<br \/>\ninstance[:tags].each do |tag|<br \/>\nfile=File.new(&#8216;test.csv&#8217;,&#8217;a+&#8217;)<br \/>\nfile.write tag[:value] + &quot;\\t&quot;<br \/>\nfile.write instance[:private_ip_address] + &quot;\\t&quot;<br \/>\nfile.write instance[:image_id] + &quot;\\t&quot;<br \/>\nfile.write instance[:state][:name] + &quot;\\t&quot;<br \/>\nfile.write instance[:instance_type] + &quot;\\t&quot;<br \/>\nfile.write instance[:architecture] + &quot;\\t&quot;<br \/>\nfile.write instance[:hypervisor] + &quot;\\t&quot;<br \/>\nfile.write instance[:client_token] + &quot;\\t&quot;<br \/>\nfile.write instance[:key_name] + &quot;\\t&quot;<br \/>\nfile.write instance[:root_device_type] + &quot;\\t&quot;<br \/>\nfile.write instance[:state][:name] + &quot;\\t&quot;<br \/>\nfile.write instance[:virtualization_type] + &quot;\\t&quot;<br \/>\nfile.puts<br \/>\nend<br \/>\nend<br \/>\nend[\/sourcecode]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35347\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/data_in_csv.png\" alt=\"data_in_csv\" width=\"954\" height=\"124\" \/><\/p>\n<p>Hope this blog gave you a kick-start on using AWS Ruby SDK. I will be coming up with more such blogs soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the years, Amazon has been integrating its web services with various programming languages to make it easier for developers to utilize\u00a0various 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 [&hellip;]<\/p>\n","protected":false},"author":913,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1174,2348],"tags":[248,1547,3528,3527,3442,3524,3526,3525,3523,980,3522],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35257"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/913"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=35257"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35257\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=35257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=35257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=35257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}