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

#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



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

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



54
55
56
# File 'lib/vscripts/aws/ec2.rb', line 54

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



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

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



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

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::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



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

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