> | | | > Hello World of AWS API with Ruby

Hello World of AWS API with Ruby

Posted on Thursday, January 31, 2013 | 2 Comments

After years of writing Perl I need to start learning Ruby. The most comprehensive SDK for Amazon Web Services (AWS) looks to be the Ruby SDK. It even contains interfaces for the new Elastic Transcoder service released overnight.

It is pleasing to find just how little it takes to create a "Hello AWS" style Ruby program. For my first test I decide that listing out my S3 buckets would be sufficient.

Here are four simple steps to get you started.

Step 1. Get Ruby on your machine.

Well of course you are using a recent Mac and Ruby is already installed. To confirm

machine:~ rodos$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
machine:~ rodos$
Step 2. Install the AWS Ruby SDK 

This is a simple command
machine:~ rodos$ sudo gem install aws-sdk
Password:

Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed uuidtools-2.1.3
Successfully installed nokogiri-1.5.6
Successfully installed json-1.7.6
Successfully installed aws-sdk-1.8.1.1
4 gems installed
Installing ri documentation for uuidtools-2.1.3...
Installing ri documentation for nokogiri-1.5.6...
No definition for parse_memory
No definition for parse_file
No definition for parse_with
No definition for get_options
No definition for set_options
Installing ri documentation for json-1.7.6...
Installing ri documentation for aws-sdk-1.8.1.1...
Installing RDoc documentation for uuidtools-2.1.3...
Installing RDoc documentation for nokogiri-1.5.6...
No definition for parse_memory
No definition for parse_file
No definition for parse_with
No definition for get_options
No definition for set_options
Installing RDoc documentation for json-1.7.6...
Installing RDoc documentation for aws-sdk-1.8.1.1...
machine:~ rodos$
Step 3. Create your .rb file with the code.

Create a file with the text below. I named my file "hello-aws.rb". Of course I still use vi for some silly reason to write code! You could always try pico or one of those fancy graphical text editors, even Xcode.
# List you S3 buckets

require 'rubygems'
require 'yaml'
require 'aws-sdk'

AWS.config(
:access_key_id => 'your.access.key.here',
:secret_access_key => 'your.secret.here')

s3 = AWS::S3.new

s3.buckets.each do |bucket|
puts bucket.name
end
Don't forget to enter your access key and secret for your account.

Step 4. Execute your file.

You can now run your code and watch it list your buckets.
machine:~ rodos$ ruby s3list.rb 
rodos.singapore.bucket1
rodos.singapore.bucket2
rodos.sydney.bucket1
machine:~ rodos$ 
There you go, it lists all of my buckets. From here it is developing your knowledge of the AWS SDK for Ruby alongside general Ruby programming skills. As I write some interesting code, I will share my experiences here.

Why don't you go and try your first automation of the Cloud with AWS and Ruby! Its fun and easy.

Rodos

P.S. It is a bad idea to leave your account access key and secret locked away in your code file. I have done it here to show a complete working example in a single file.

Comments:2

  1. Anonymous9:02 am

    first you mention your script is hello-aws.rb and then its s3list.rb !!

    ReplyDelete
  2. That's a good point. Editorial error.

    ReplyDelete

Powered by Blogger.