Module: AwsWatcher::EC2

Defined in:
lib/aws-watcher.rb

Class Method Summary collapse

Class Method Details

.client(config) ⇒ Object



22
23
24
# File 'lib/aws-watcher.rb', line 22

def self.client(config)
  Aws::EC2::Client.new(config[:aws])
end

.instance_alive?(id, config) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aws-watcher.rb', line 26

def self.instance_alive?(id, config)
  AwsWatcher::EC2.client(config).describe_instance_attribute(
    attribute: 'instanceType',
    instance_id: id.to_s
  )
  true
rescue Aws::EC2::Errors::InvalidInstanceIDNotFound => e
  p "instance: #{id} was not found."
  if ENV['LOG_LEVEL'] == 'DEBUG'
    p e
  end
  false
end

.tag_matches?(id, tag, values, config) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/aws-watcher.rb', line 40

def self.tag_matches?(id, tag, values, config)
  instances = AwsWatcher::EC2.client(config).describe_instances(
    instance_ids: [id],
    filters: [
      {
        name: "tag:#{tag}",
        values: values.to_a
      }
    ]
  )
  if instances.reservations.empty?
    false
  else
    true
  end
end