Module: Vrt::Ec2::Tags

Defined in:
lib/vrt/ec2/tags.rb,
lib/vrt/ec2/tags/version.rb

Overview

This module will fetch the EC2 tags for this instance via custom lambda.

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.instance_idObject



53
54
55
56
57
58
59
60
# File 'lib/vrt/ec2/tags.rb', line 53

def self.instance_id
  id = read_instance_id
  raise('Something bad happened since there was no error but this is not a string.') unless id.is_a? String

  id
rescue StandardError
  warn 'This is not an AWS EC2 instance or unable to contact the AWS instance-data web server.'
end

.meta_data_uriObject



19
20
21
# File 'lib/vrt/ec2/tags.rb', line 19

def self.
  @meta_data_uri || @meta_data_uri = URI.parse('http://169.254.169.254')
end

.read_instance_idObject



62
63
64
65
66
67
68
# File 'lib/vrt/ec2/tags.rb', line 62

def self.read_instance_id
  http = Net::HTTP.new(.host, .port)
  http.open_timeout = 2
  http.read_timeout = 4
  request = Net::HTTP::Get.new('/latest/dynamic/instance-identity/rsa2048')
  http.request(request).body
end

.read_tagsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vrt/ec2/tags.rb', line 27

def self.read_tags
  response = request_tags
  case response
  when Net::HTTPSuccess
    response.body
  when Net::HTTPUnauthorized
    raise("#{response.message}: username and password set and correct?")
  when Net::HTTPServerError
    raise("#{response.message}: try again later?")
  else
    raise(response.message)
  end
end

.request_tagsObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vrt/ec2/tags.rb', line 41

def self.request_tags
  http = Net::HTTP.new(tag_uri.host, tag_uri.port)
  http.use_ssl = true
  http.open_timeout = 2
  http.read_timeout = 4
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  request = Net::HTTP::Post.new(tag_uri)
  request.add_field 'Content-Type', 'text/plain; charset=utf-8'
  request.body = instance_id
  http.request(request)
end

.tag_uriObject



23
24
25
# File 'lib/vrt/ec2/tags.rb', line 23

def self.tag_uri
  @tag_uri || @tag_uri = URI.parse('https://cert-lambda.core.a51.be/tags')
end

.tagsObject



12
13
14
15
16
17
# File 'lib/vrt/ec2/tags.rb', line 12

def self.tags
  yamlstring = read_tags
  puts yamlstring if yamlstring
rescue StandardError => e
  warn "Something failed.... Abort! : #{e.class} - #{e.message}"
end