Last active
December 23, 2018 14:12
-
-
Save mdub/5654700 to your computer and use it in GitHub Desktop.
A simple Ruby-based AWS console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
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
Nice, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A number of recent changes to the aws-sdk can greatly simplify this gist.
:region
option, no need to specify endpoints for each serviceENV['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 callings3
multiple times will create a newAWS::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:The region will not default to 'ap-southeast-2'. You can maintain this by adding: