Skip to content

Instantly share code, notes, and snippets.

@mdub
Last active December 23, 2018 14:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mdub/5654700 to your computer and use it in GitHub Desktop.
Save mdub/5654700 to your computer and use it in GitHub Desktop.
A simple Ruby-based AWS console
#! /usr/bin/env ruby
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
require 'aws-sdk'
access_key_id = ENV.fetch("AWS_ACCESS_KEY_ID")
secret_access_key = ENV.fetch("AWS_SECRET_ACCESS_KEY")
region = ENV.fetch("AWS_DEFAULT_REGION", "ap-southeast-2")
config = {
:access_key_id => access_key_id,
:secret_access_key => secret_access_key,
:cloud_formation_endpoint => "cloudformation.#{region}.amazonaws.com",
:ec2_endpoint => "ec2.#{region}.amazonaws.com",
:elastic_beanstalk_endpoint => "elasticbeanstalk.#{region}.amazonaws.com"
}
AWS.config(config)
class AWSContext
def self.service(name, klass)
define_method(name) do
@services[name] ||= klass.new
end
end
def initialize
@services = {}
end
service :cloud_formation, AWS::CloudFormation
service :elastic_beanstalk, AWS::ElasticBeanstalk
service :ec2, AWS::EC2
service :s3, AWS::S3
end
require 'pry'
Pry.config.prompt = [proc { "AWS> " }, proc { "AWS| " }]
AWSContext.new.pry
@trevorrowe
Copy link

A number of recent changes to the aws-sdk can greatly simplify this gist.

  • AWS now provides a helper method for each service (e.g. AWS.s3, AWS.ec2, AWS.elastic_beanstalk, etc)
  • AWS.config now accepts a single :region option, no need to specify endpoints for each service
  • AWS.config now defaults #region to ENV['AWS_REGION'] (this is similar to what you are doing)

@lsegal forked this gist showing how simple this becomes: https://gist.github.com/lsegal/5681466 for an example.

The primary differences between this gist and @lsegal's are:

  • Service interfaces are not cached, like they are in the AWSContext. Instead calling s3 multiple times will create a new AWS::S3 object. This should be just fine. HTTP connections are still persistent (they are managed in a shared connection pool), so there is very little overhead in creating a new service interface. This also has the benefit of getting updates from AWS.config. This allows me to do the following:

     s3.buckets.first
     AWS.config(logger: Logger.new($stdout))
     s3.buckets.first # now with logging!
    
  • The region will not default to 'ap-southeast-2'. You can maintain this by adding:

    AWS.config(region: ENV.fetch("AWS_DEFAULT_REGION", "ap-southeast-2"))
    

@trevorrowe
Copy link

Here is another version to play with. This adds a colored logger by default and makes it easy to enable a debug mode which prints HTTP wire traces.

#!/usr/bin/env ruby

# silence warnings from pry, these are normally autoloaded by aws-sdk
require 'nokogiri'
require 'json'
require 'uuidtools'

require 'aws-sdk'
require 'logger'
require 'pry'

AWS.config(
  logger: ENV['NO_LOG'] ? nil : Logger.new($stdout),
  log_formatter: AWS::Core::LogFormatter.send(ENV['LOGGER'] || 'colored'),
  http_wire_trace: !!ENV['DEBUG'] || $DEBUG)

Pry.config.prompt = [proc { "AWS> " }, proc { "AWS| " }]

AWS.pry

@mdub
Copy link
Author

mdub commented Jun 3, 2013

Nice, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment