Last active
August 29, 2015 14:14
-
-
Save y13i/a0253efe9dbae74f7bc1 to your computer and use it in GitHub Desktop.
ThorでAWSごにょごにょする時にいつも書いてる気がするやつ
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
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