Module: VScripts::AWS::EC2

Included in:
VScripts::AWS
Defined in:
lib/vscripts/aws/ec2.rb

Overview

A collection of methods used for interaction with Amazon Web Service Elastic Compute Cloud.

Instance Method Summary collapse

Instance Method Details

#all_tagsAWS::EC2::ResourceTagCollection



22
23
24
# File 'lib/vscripts/aws/ec2.rb', line 22

def all_tags
  instance.tags
end

#all_tags_hashHash



42
43
44
45
46
47
48
# File 'lib/vscripts/aws/ec2.rb', line 42

def all_tags_hash
  hash = {}
  all_tags.each do |key, value|
    hash = hash.merge(Hash[key, value])
  end
  hash
end

#create_tag(resource, key, value) ⇒ AWS::EC2::Tag

Creates an EC2 Tag



37
38
39
# File 'lib/vscripts/aws/ec2.rb', line 37

def create_tag(resource, key, value)
  ec2.tags.create(resource, key, value)
end

#ec2Object

Loads AWS SDK for EC2



11
12
13
# File 'lib/vscripts/aws/ec2.rb', line 11

def ec2
  ::AWS::EC2.new(region: region)
end

#functional_instancesAWS::EC2::InstanceCollection



73
74
75
76
77
78
# File 'lib/vscripts/aws/ec2.rb', line 73

def functional_instances
  named_instances.map do |named_instance|
    named_instance if [:running, :shutting_down, :stopping, :stopped]
      .include?(named_instance.status)
  end
end

#instanceAWS::EC2::Instance



16
17
18
19
# File 'lib/vscripts/aws/ec2.rb', line 16

def instance
  check_instance
  ec2.instances[instance_id]
end

#nameString

Looks for the value of the ‘Name’ tag for the given instance



63
64
65
# File 'lib/vscripts/aws/ec2.rb', line 63

def name
  all_tags_hash['Name']
end

#named_instancesAWS::EC2::InstanceCollection



68
69
70
# File 'lib/vscripts/aws/ec2.rb', line 68

def named_instances
  ec2.instances.tagged('Name')
end

#similar_instancesAWS::EC2::InstanceCollection



82
83
84
85
86
87
88
# File 'lib/vscripts/aws/ec2.rb', line 82

def similar_instances
  check_instance
  functional_instances.compact.map do |functional_instance|
    next if functional_instance.id == instance_id
    functional_instance.tags['Name']
  end
end

#tag(key) ⇒ AWS::EC2::Tag



28
29
30
# File 'lib/vscripts/aws/ec2.rb', line 28

def tag(key)
  instance.tags[key]
end

#tags_without(list = []) ⇒ Hash

Exclude tags



53
54
55
56
57
58
59
# File 'lib/vscripts/aws/ec2.rb', line 53

def tags_without(list = [])
  hash = all_tags_hash
  list.each do |key|
    hash.delete(key)
  end
  hash
end