Class: AwsData

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/aws_data.rb,
lib/aws_data/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#accountObject

aws sts get-caller-identity



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aws_data.rb', line 52

def 
  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.
  rescue Aws::Errors::MissingCredentialsError
    puts "INFO: You're missing AWS credentials. Only local services are currently available"
  end
end

#regionObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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