Class: AwsCftTools::Client::EC2

Inherits:
Base
  • Object
show all
Defined in:
lib/aws_cft_tools/client/ec2.rb

Overview

EC2 Instance Client

All of the business logic behind direct interaction with the AWS API for EC2 instances and related resources.

Instance Attribute Summary

Attributes inherited from Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#aws_client

Constructor Details

#initialize(options) ⇒ EC2

Returns a new instance of EC2.

Parameters:

  • options (Hash)

    client configuration

Options Hash (options):

  • :environment (String)

    the operational environment in which to act

  • :profile (String)

    the AWS credential profile to use

  • :region (String)

    the AWS region in which to act

  • :role (String)

    the operational role of the resources under consideration



20
21
22
# File 'lib/aws_cft_tools/client/ec2.rb', line 20

def initialize(options)
  super(options)
end

Class Method Details

.aws_client_classObject



24
25
26
# File 'lib/aws_cft_tools/client/ec2.rb', line 24

def self.aws_client_class
  Aws::EC2::Resource
end

Instance Method Details

#imagesArray<OpenStruct>

Returns a list of available AMI images filtered by any environment or role specified in the options passed to the constructor.

Each image is represented by an OpenStruct with the following keys/methods:

  • image_id: the ID of the AMI or image

  • type: the type of AMI or image

  • public: a flag indicating if the image is public

  • created_at: the date/time at which the image was created

  • role: the value of the ‘Role` tag if not filtering by role

  • environment: the value of the ‘Environment` tag if not filtering by environment

Returns:

  • (Array<OpenStruct>)


65
66
67
68
69
70
71
72
73
74
# File 'lib/aws_cft_tools/client/ec2.rb', line 65

def images
  @images ||= aws_client.images(owners: ['self'], filters: image_filters).map do |image|
    OpenStruct.new(
      with_tags(image, image_id: image.image_id,
                       type: image.image_type,
                       public: image.public,
                       created_at: image.creation_date)
    )
  end
end

#instancesArray<OpenStruct>

Returns a list of running instances filtered by any environment or role specified in the options passed to the constructor.

Each instance is represented by a Hash with the following keys:

  • private_ip: the private IP address of the instance

  • public_ip: the public IP address (if any) of the instance

  • instance: the ID of the instance

  • role: the value of the ‘Role` tag if not filtering by role

  • environment: the value of the ‘Environment` tag if not filtering by environment

Returns:

  • (Array<OpenStruct>)


41
42
43
44
45
46
47
48
49
# File 'lib/aws_cft_tools/client/ec2.rb', line 41

def instances
  @instances ||= aws_client.instances(filters: instance_filters).map do |instance|
    OpenStruct.new(
      with_tags(instance, private_ip: instance.private_ip_address,
                          public_ip: instance.public_ip_address,
                          instance: instance.instance_id)
    )
  end
end