class AwsData
Constants
- VERSION
Public Instance Methods
account()
click to toggle source
aws sts get-caller-identity
# File lib/aws_data.rb, line 52 def account return '123456789' if test? return ENV['AWS_ACCOUNT'] if ENV['AWS_ACCOUNT'] # ensure region set, required for sts.get_caller_identity.account to work ENV['AWS_REGION'] ||= region begin sts.get_caller_identity.account rescue Aws::Errors::MissingCredentialsError puts "INFO: You're missing AWS credentials. Only local services are currently available" end end
region()
click to toggle source
# File lib/aws_data.rb, line 10 def region return 'us-east-1' if test? return ENV['AWS_REGION'] if ENV['AWS_REGION'] # highest precedence region = nil # First if aws binary is available # try to get it from the ~/.aws/config if which('aws') region = `aws configure get region 2>&1`.strip rescue nil exit_code = $?.exitstatus if exit_code != 0 exception_message = region.split("\n").grep(/botocore\.exceptions/).first if exception_message puts "WARN: #{exception_message}".color(:yellow) else # show full message as warning puts region.color(:yellow) end puts "You can also get rid of this message by setting AWS_REGION or configuring ~/.aws/config with the region" region = nil end region = nil if region == '' return region if region end # Second try the metadata endpoint, should be available on AWS Lambda environment # https://stackoverflow.com/questions/4249488/find-region-from-within-an-ec2-instance begin az = `curl -s --max-time 3 --connect-timeout 5 http://169.254.169.254/latest/meta-data/placement/availability-zone` region = az.strip.chop # remove last char region = nil if region == '' rescue end return region if region 'us-east-1' # default if all else fails end
Private Instance Methods
sts()
click to toggle source
# File lib/aws_data.rb, line 87 def sts Aws::STS::Client.new end
test?()
click to toggle source
# File lib/aws_data.rb, line 67 def test? ENV['AWS_DATA_TEST'] end
which(cmd)
click to toggle source
Cross-platform way of finding an executable in the $PATH.
which('ruby') #=> /usr/bin/ruby
source: stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
# File lib/aws_data.rb, line 76 def which(cmd) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each { |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) } end return nil end