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

Returns all tags.

Returns:

  • (AWS::EC2::ResourceTagCollection)

    all tags



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

def all_tags
  instance.tags
end

#all_tags_hashHash

Returns a correct Hash object with all tags and keys.

Returns:

  • (Hash)

    a correct Hash object with all tags and keys



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

Parameters:

  • resource (String)

    the id of the resource

  • key (String)

    the key of the tag

  • value (String)

    the value of the tag

Returns:

  • (AWS::EC2::Tag)

    the new 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

Returns instances that are not terminated.

Returns:

  • (AWS::EC2::InstanceCollection)

    instances that are not terminated



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

Returns the current instance.

Returns:

  • (AWS::EC2::Instance)

    the current 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

Returns:

  • (String)

    the name value



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

def name
  all_tags_hash['Name']
end

#named_instancesAWS::EC2::InstanceCollection

Returns the value of the name tag.

Returns:

  • (AWS::EC2::InstanceCollection)

    the value of the name tag



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

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

#similar_instancesAWS::EC2::InstanceCollection

Returns running instances that have a Name tag.

Returns:

  • (AWS::EC2::InstanceCollection)

    running instances that have a Name tag



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

Returns the value of the tag.

Parameters:

  • key (String)

    the key

Returns:

  • (AWS::EC2::Tag)

    the value of the 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

Parameters:

  • list (Array) (defaults to: [])

    the list of tag keys to be excluded

Returns:

  • (Hash)

    the filtered 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