Skip to content

Instantly share code, notes, and snippets.

@y13i
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y13i/a0253efe9dbae74f7bc1 to your computer and use it in GitHub Desktop.
Save y13i/a0253efe9dbae74f7bc1 to your computer and use it in GitHub Desktop.
ThorでAWSごにょごにょする時にいつも書いてる気がするやつ
require "thor"
require "aws-sdk-resources"
class OrenoAwsTool < Thor
class_option :profile,
desc: "Load credentials by profile name from shared credentials file (~/.aws/credentials).",
aliases: [:p]
class_option :access_key_id,
desc: "AWS access key id.",
aliases: [:k]
class_option :secret_access_key,
desc: "AWS secret access key.",
aliases: [:s]
class_option :region,
desc: "AWS region.",
aliases: [:r]
# 中略
private
def aws_configuration
hash = {}
[:profile, :access_key_id, :secret_access_key, :region].each do |option|
hash.update(option => options[option]) if options[option]
end
hash.update(region: own_region) if hash[:region].nil? && ENV["AWS_REGION"].nil?
hash
end
def own_region
@own_region ||= begin
require "net/http"
timeout 3 do
Net::HTTP.get("169.254.169.254", "/latest/meta-data/placement/availability-zone").chop
end
rescue
nil
end
end
def ec2
@ec2 ||= Aws::EC2::Resource.new aws_configuration
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment