AWS Developer Tools Blog

Using the AWS SDK for Ruby from Your REPL

We are all used to spinning up irb or Pry sessions to play with Ruby’s features interactively. Some people reading this might even be using the rails console on a daily basis, which can make digging through Ruby on Rails applications much easier. Well, we’re actually working on bringing that same functionality into the AWS SDK for Ruby!

The Backstory

We’ve been using an internal version of our interactive console for a long time in order to more easily develop and debug new features in the Ruby SDK, but it’s always been a very homegrown and customized tool. We didn’t feel that it was in the right state to be published along with the SDK, but we were also looking at extracting this out as a public executable in the aws-sdk gem that we are comfortable supporting.

And then a couple of weeks ago, a developer by the name of Mike Williams (@woollyams on Twitter) posted a Gist that showed how to launch a REPL for the Ruby SDK using Pry, which got us thinking about (and working on) extracting our REPL a little bit more.

Tweet about the Pry REPL (@woollyams)

Thanks, Mike, for indirectly helping to move this forward!

Introducing the REPL

Trevor took the above Gist and did some refactoring to make it work with other services, as well as play more nicely with some of the new convenience features in the Ruby SDK. The end result is now sitting in a branch on the aws/aws-sdk-ruby repository (aws-sdk-repl). You can try the REPL out for yourself by checking out the repository and running ./bin/aws-rb:

$ git clone git://github.com/aws/aws-sdk-ruby
$ cd aws-sdk-ruby
$ git checkout aws-sdk-repl
$ ./bin/aws-rb --help
Usage: aws-rb [options]
        --repl REPL                  specify the repl environment, pry or irb
    -l, --[no-]log                   log client requets, on by default
    -c, --[no-]color                 colorize request logging, on by default
    -d, --[no-]debug                 log HTTP wire traces, off by default
    -Idirectory                      specify $LOAD_PATH directory (may be used more than once)
    -rlibrary                        require the library
    -v, --verbose                    enable client logging and HTTP wire tracing
    -q, --quiet                      disable client logging and HTTP wire tracing
    -h, --help

The Features

Pry by default

The tool currently attempts to use Pry by default, if available, and falls back to a plain old irb session, if it is not. If you want to stick to irb or Pry, pass --repl irb or --repl pry respectively. You can also set this through environment variables (more information on this is discussed in the pull request).

Logging interactively

Also by default, we show simple logging for each request that the SDK sends. This can shed a lot of light onto how you are using the SDK. For example, when you list buckets from S3, you might see:

AWS> s3.buckets.map(&:name)
[AWS S3 200 0.933647 0 retries] list_buckets()  
=> ["mybucket1", "mybucket2", "mybucket3", ...]

You can also show HTTP wire traces by passing -d to the console to run in debug mode. These values can all also be set through environment variables.

Logging existing scripts

Finally, if you have a small script that you want to debug or profile, you can use the aws-rb shell to quickly log all requests and ensure that the right things are happening in that script:

$ cat test.rb
require 'aws-sdk'
AWS.s3.buckets.to_a
AWS.sqs.queues.to_a
$ ./bin/aws-rb -d -I . -r test.rb
<...WIRE TRACE DATA HERE...>
[AWS S3 200 1.183635 0 retries] list_buckets()  
<...WIRE TRACE DATA HERE...>
[AWS SQS 200 0.836059 0 retries] list_queues()  

Looks like we got the requests we were looking for!

Making It Live

We are currently crossing our t’s and dotting our i’s on this new feature, but if you have feedback that you would like to get in on this new REPL, feel free to jump in on the pull request #270 to comment. We’d love to hear anything you have to say, from any large feature omissions all the way down to suggestions for the executable name. Please join in on the conversation at GitHub.