class InstanceMetadata

Constants

IP_ADDRESS
PORT

Public Class Methods

host_identifier() click to toggle source
# File lib/instance_metadata.rb, line 10
def self.host_identifier
  doc = JSON.parse(http_get('/latest/dynamic/instance-identity/document').strip)
  "arn:aws:ec2:#{doc['region']}:#{doc['accountId']}:instance/#{doc['instanceId']}"
end
instance_id() click to toggle source
# File lib/instance_metadata.rb, line 22
def self.instance_id
  begin
    Net::HTTP.start(IP_ADDRESS, PORT) do |http|
      response = http.get('/latest/meta-data/instance-id')
      if response.code.to_i != 200
        return nil
      end
      return response.body
    end
  rescue
    return nil
  end
end
region() click to toggle source
# File lib/instance_metadata.rb, line 15
def self.region
  az = http_get('/latest/meta-data/placement/availability-zone').strip
  raise "Invalid availability zone name: #{az}" unless
    az =~ /[a-z]{2}-[a-z]+-\d+[a-z]/
  az.chop
end

Private Class Methods

http_get(path) click to toggle source
# File lib/instance_metadata.rb, line 37
def self.http_get(path)
  Net::HTTP.start(IP_ADDRESS, PORT) do |http|
    response = http.get(path)
    if response.code.to_i != 200
      InstanceAgent::Log.send(:debug, "HTTP error from metadata service, code #{response.code}")
      raise "HTTP error from metadata service, code #{response.code}"
    end
    return response.body
  end
end