Module: VScripts::AWS::EC2

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

Overview

AWS Elastic Compute Cloud

Instance Method Summary collapse

Instance Method Details

#all_tagsAWS::EC2::ResourceTagCollection

Returns Tags collection.

Returns:

  • (AWS::EC2::ResourceTagCollection)

    Tags collection



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

def all_tags
  instance.tags
end

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

Returns Creates an EC2 Tag.

Returns:

  • (AWS::EC2::Tag)

    Creates an EC2 Tag



31
32
33
# File 'lib/vscripts/aws/ec2.rb', line 31

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

#ec2Object

Load AWS SDK for EC2



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

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

#functional_instancesObject

@return Lists instances that are not terminated



56
57
58
59
60
61
# File 'lib/vscripts/aws/ec2.rb', line 56

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

#instanceObject

Get instance object



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

def instance
  check_instance
  ec2.instances[instance_id]
end

#nameObject

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



45
46
47
# File 'lib/vscripts/aws/ec2.rb', line 45

def name
  all_tags_hash['Name']
end

#named_instancesObject

@return Lists instances that have a ‘Name’ tag



51
52
53
# File 'lib/vscripts/aws/ec2.rb', line 51

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

#similar_instancesObject

@return The ‘Name’ tag value except the local instance



64
65
66
67
68
69
70
# File 'lib/vscripts/aws/ec2.rb', line 64

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

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

Returns Tags collection.

Returns:

  • (AWS::EC2::ResourceTagCollection)

    Tags collection



26
27
28
# File 'lib/vscripts/aws/ec2.rb', line 26

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

#tags_without(list = []) ⇒ Object

Get a list of tags



36
37
38
39
40
41
42
# File 'lib/vscripts/aws/ec2.rb', line 36

def tags_without(list = [])
  all_tags.each_with_object({}) do |tag, hash|
    key, value = tag[0], tag[1]
    hash[key] = value unless list.include? key
    hash
  end
end