module AwsInfo

Constants

DEFAULT_REGION
VERSION

Public Class Methods

account_id() click to toggle source
# File lib/aws_info/aws_info.rb, line 54
def account_id
  info["accountId"]
end
architecture() click to toggle source
# File lib/aws_info/aws_info.rb, line 74
def architecture
  info["architecture"]
end
availability_zone() click to toggle source
# File lib/aws_info/aws_info.rb, line 30
def availability_zone
  info["availabilityZone"]
end
billing_products() click to toggle source
# File lib/aws_info/aws_info.rb, line 50
def billing_products
  info["billingProducts"]
end
dev_pay_product_codes() click to toggle source
# File lib/aws_info/aws_info.rb, line 46
def dev_pay_product_codes
  info["devpayProductCodes"]
end
image_id() click to toggle source
# File lib/aws_info/aws_info.rb, line 62
def image_id
  info["imageId"]
end
info() click to toggle source
# File lib/aws_info/aws_info.rb, line 14
def info
  if @info.nil? || @info == {}
    @info = load_meta_data
  else
    @info
  end
end
instance_id() click to toggle source
# File lib/aws_info/aws_info.rb, line 34
def instance_id
  info["instanceId"]
end
instance_type() click to toggle source
# File lib/aws_info/aws_info.rb, line 38
def instance_type
  info["instanceType"]
end
ip() click to toggle source
# File lib/aws_info/aws_info.rb, line 26
def ip
  info["privateIp"]
end
kernel_id() click to toggle source
# File lib/aws_info/aws_info.rb, line 66
def kernel_id
  info["kernelId"]
end
pending_time() click to toggle source
# File lib/aws_info/aws_info.rb, line 58
def pending_time
  info["pendingTime"]
end
ram_disk_id() click to toggle source
# File lib/aws_info/aws_info.rb, line 70
def ram_disk_id
  info["ramdiskId"]
end
region() click to toggle source
# File lib/aws_info/aws_info.rb, line 22
def region
  info["region"] || DEFAULT_REGION
end
tags() click to toggle source
# File lib/aws_info/aws_info.rb, line 10
def tags
  @tags ||= load_tag_data
end
version() click to toggle source
# File lib/aws_info/aws_info.rb, line 42
def version
  info["version"]
end

Private Class Methods

load_meta_data() click to toggle source
# File lib/aws_info/aws_info.rb, line 94
def load_meta_data
  JSON.parse(query_instance_identity)
rescue => e
  puts 'Could not load AwsInfo. Is 169.254.169.254 reachable?'
  {}
end
load_tag_data() click to toggle source
# File lib/aws_info/aws_info.rb, line 82
def load_tag_data
  tag_array = JSON.parse(query_tag_data)["Tags"] rescue []
  tag_array.inject({}) { |tags, e| tags[e["Key"]] = e["Value"]; tags }
end
query_instance_identity() click to toggle source
# File lib/aws_info/aws_info.rb, line 101
def query_instance_identity
  Timeout::timeout(1) {
    url = 'http://169.254.169.254/latest/dynamic/instance-identity/document'
    Net::HTTP.get(URI.parse(url))
  }
end
query_tag_data() click to toggle source
# File lib/aws_info/aws_info.rb, line 87
def query_tag_data
  # You must have permissions to run aws ec2 describe-tags
  # Is there a better way to get this without relying on sysyem calls?
  # Aws sdk provides this, but do I want to rely on the aws-adk?
  `aws ec2 describe-tags --region #{region} --filter "Name=resource-id,Values=#{instance_id}"`
end