class Ec2Templater::FilteredEc2List

Public Class Methods

new(client = Aws::EC2::Client.new) click to toggle source

Provide an awssdk v2 client

# File lib/ec2_templater/filtered_ec2_list.rb, line 6
def initialize(client = Aws::EC2::Client.new)
  @client = client
end

Public Instance Methods

[](filters = {}) click to toggle source
# File lib/ec2_templater/filtered_ec2_list.rb, line 26
def [](filters = {})
  call(filters)
end
call(filters = {}) click to toggle source

Return list of instances that match the provided filters.

The filters match those defined for the describe-instances api call. docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html

The returned instances are <Types::Instance> from awssdk. See: docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Types/Instance.html

Filter Examples:

{ "tag:Name" => "", "vpc-id" => "vpc-9321c9a2" }
{ "tag:Environment" => "Production" }
# File lib/ec2_templater/filtered_ec2_list.rb, line 21
def call(filters = {})
  args = { filters: processed_filters(default_filters.merge(filters)) }
  @client.describe_instances(args).reservations.flat_map(&:instances)
end
default_filters() click to toggle source
# File lib/ec2_templater/filtered_ec2_list.rb, line 36
def default_filters
  { 'instance-state-name' => 'running' }
end
processed_filters(filters) click to toggle source
# File lib/ec2_templater/filtered_ec2_list.rb, line 30
def processed_filters(filters)
  filters.map do |key, value|
    { name: key, values: Array(value) }
  end
end