Hello World of AWS API with Ruby
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 -vStep 2. Install the AWS Ruby SDK
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
machine:~ rodos$
This is a simple command
machine:~ rodos$ sudo gem install aws-sdkStep 3. Create your .rb file with the code.
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$
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 bucketsDon't forget to enter your access key and secret for your account.
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
Step 4. Execute your file.
You can now run your code and watch it list your buckets.
machine:~ rodos$ ruby s3list.rbThere 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.
rodos.singapore.bucket1
rodos.singapore.bucket2
rodos.sydney.bucket1
machine:~ rodos$
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.
With over 20 years working in the IT industry I have had varied sub careers. My first decade was as a programmer, developing applications in CASE tools for major corporations in Asia. There was the obligatory dotcom involvement in a start up. For the last decade I have worked in the systems integration space as a consultant. Today my focus and passion is on Cloud and virtualisation. I spend my time doing strategy, evangelism and architecture.
first you mention your script is hello-aws.rb and then its s3list.rb !!
ReplyDeleteThat's a good point. Editorial error.
ReplyDelete