Instantly share code, notes, and snippets.

@y13i /thor_aws.rb
Last active Aug 29, 2015

Embed
What would you like to do?
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