class AwsCftTools::Client::EC2

EC2 Instance Client

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

Public Class Methods

aws_client_class() click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 24
def self.aws_client_class
  Aws::EC2::Resource
end
new(options) click to toggle source

@param options [Hash] client configuration @option options [String] :environment the operational environment in which to act @option options [String] :profile the AWS credential profile to use @option options [String] :region the AWS region in which to act @option options [String] :role the operational role of the resources under consideration

Calls superclass method AwsCftTools::Client::Base::new
# File lib/aws_cft_tools/client/ec2.rb, line 20
def initialize(options)
  super(options)
end

Public Instance Methods

images() click to toggle source

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

@return [Array<OpenStruct>]

# 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
instances() click to toggle source

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

@return [Array<OpenStruct>]

# 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

Private Instance Methods

arbitrary_tag_filters() click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 110
def arbitrary_tag_filters
  tags = options[:tags] || []
  tags.inject([]) do |filter_set, tag_value|
    tag, value = tag_value
    filter_set << { name: "tag:#{tag}", values: [value] }
  end
end
environment_filter() click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 102
def environment_filter
  tag_filter('Environment', options[:environment])
end
image_filters() click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 86
def image_filters
  @image_filters ||= begin
    [
      { name: 'state', values: ['available'] }
    ] + tag_filters
  end
end
instance_filters() click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 78
def instance_filters
  @instance_filters ||= begin
    [
      { name: 'instance-state-name', values: ['running'] }
    ] + tag_filters
  end
end
role_filter() click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 106
def role_filter
  tag_filter('Role', options[:role])
end
tag_filter(tag, value) click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 118
def tag_filter(tag, value)
  if value
    [{ name: "tag:#{tag}", values: [value] }]
  else
    []
  end
end
tag_filters() click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 94
def tag_filters
  @tag_filters ||= begin
    environment_filter +
      role_filter +
      arbitrary_tag_filters
  end
end
with_tags(resource, info = {}) click to toggle source
# File lib/aws_cft_tools/client/ec2.rb, line 126
def with_tags(resource, info = {})
  tags = resource.tags.each_with_object({}) { |tag, collection| collection[tag.key] = tag.value }
  info.merge(
    role: tags.delete('Role'),
    environment: tags.delete('Environment'),
    tags: tags
  )
end