module Cumulus::EC2

Constants

InstanceAttributes

Easily load instance attribute data individually and lazy

VolumeGroup

Public Class Methods

addresses() click to toggle source

Public: Lazily load the addresses

# File lib/ec2/EC2.rb, line 227
def addresses
  @addresses ||= init_addresses
end
dhcp_options() click to toggle source

Public: Lazily load the dhcp options

# File lib/ec2/EC2.rb, line 191
def dhcp_options
  @dhcp_options ||= init_dhcp_options
end
ebs_groups() click to toggle source

Public

Returns an array of the group names used by all ebs volumes

# File lib/ec2/EC2.rb, line 283
def ebs_groups
  @ebs_groups ||= ebs_volumes.map(&:group).reject(&:nil?).uniq
end
ebs_volumes() click to toggle source

Public: Lazily loads EBS volumes, rejecting all root-mounted volumes

# File lib/ec2/EC2.rb, line 295
def ebs_volumes
  @ebs_volumes ||= init_ebs_volumes.reject do |vol|
    vol.attachments.any? do |att|
      attached_instance = id_instances[att.instance_id]
      attached_instance.root_device_name == att.device
    end
  end
end
endpoints() click to toggle source

Public: Lazily load the endpoints

# File lib/ec2/EC2.rb, line 205
def endpoints
  @endpoints ||= init_endpoints
end
group_ebs_volumes() click to toggle source

Public

Returns a Hash of ebs volume group name to EbsGroupConfig

# File lib/ec2/EC2.rb, line 263
def group_ebs_volumes
  @group_ebs_volumes ||= Hash[ebs_groups.map do |group_name|
    vols = ebs_volumes.select { |vol| vol.group == group_name}
    [group_name, EbsGroupConfig.new(group_name).populate!(vols)]
  end]
end
group_ebs_volumes_aws() click to toggle source

Public

Returns a Hash of ebs volume group name to Aws::EC2::Types::Volume

# File lib/ec2/EC2.rb, line 273
def group_ebs_volumes_aws
  @group_ebs_volumes_aws ||= Hash[ebs_groups.map do |group_name|
    vols = ebs_volumes.select { |vol| vol.group == group_name}
    [group_name, vols]
  end]
end
id_dhcp_options() click to toggle source

Public

Returns a Hash of dhcp options id to Aws::EC2::Types::DhcpOptions

# File lib/ec2/EC2.rb, line 186
def id_dhcp_options
  @id_dhcp_options ||= Hash[dhcp_options.map { |dhcp| [dhcp.dhcp_options_id, dhcp] }]
end
id_ebs_volumes() click to toggle source

Public

Returns a Hash of ebs volume id to volume

# File lib/ec2/EC2.rb, line 290
def id_ebs_volumes
  @id_ebs_volumes ||= Hash[ebs_volumes.map { |vol| [vol.volume_id, vol] }]
end
id_instance_attributes(instance_id) click to toggle source

Public

Returns a Hash of instance id to attributes, lazily loading each attribute

# File lib/ec2/EC2.rb, line 321
def id_instance_attributes(instance_id)
  @id_instance_attributes ||= {}
  @id_instance_attributes[instance_id] ||= InstanceAttributes.new(instance_id)
end
id_instances() click to toggle source

Public

Returns a Hash of instance id to Aws::EC2::Types::Instance

# File lib/ec2/EC2.rb, line 307
def id_instances
  @id_instances ||= Hash[instances.map { |i| [i.instance_id, i] }]
end
id_network_interfaces() click to toggle source

Public

Returns a Hash of interface id to Aws::EC2::Types::NetworkInterface

# File lib/ec2/EC2.rb, line 242
def id_network_interfaces
  @id_network_interfaces ||= Hash[network_interfaces.map { |net| [net.network_interface_id, net] }]
end
id_route_tables() click to toggle source

Public

Returns a Hash of route table id Aws::EC2::Types::RouteTable

# File lib/ec2/EC2.rb, line 131
def id_route_tables
  @id_route_tables ||= Hash[@route_tables.map { |rt| [rt.route_table_id, rt] }]
end
id_subnets() click to toggle source

Public

Returns a Hash of subnet id to Aws::EC2::Types::Subnet

# File lib/ec2/EC2.rb, line 51
def id_subnets
  @id_subnets ||= Hash[subnets.map { |subnet| [subnet.subnet_id, subnet] }]
end
id_vpcs() click to toggle source

Public

Returns a Hash of VPC id to Aws::EC2::Types::Vpc

# File lib/ec2/EC2.rb, line 88
def id_vpcs
  @vpc_ids ||= Hash[vpcs.map { |vpc| [vpc.vpc_id, vpc] }]
end
init_instance_attribute(instance_id, attribute) click to toggle source

Public: Load instance attributes

Returns the instance attribute as whatever type that attribute is

# File lib/ec2/EC2.rb, line 329
def init_instance_attribute(instance_id, attribute)
  @@client.describe_instance_attribute({
    instance_id: instance_id,
    attribute: attribute
  })
end
init_tags(object_id) click to toggle source

Public: Load tags for an ec2 object

Returns the tags as Hash

# File lib/ec2/EC2.rb, line 356
def init_tags(object_id)
  next_token = nil
  tags_loaded = false

  tags = []

  while !tags_loaded do
    resp = @@client.describe_tags({
      filters: [
        {
          name: "resource-id",
          values: [object_id]
        }
      ]
    })

    tags.concat(resp.tags)
    tags_loaded = resp.next_token.nil? || resp.next_token.empty?
    next_token = resp.next_token

  end

  Hash[tags.map { |tag|  [tag.key, tag.value] }]

end
instances() click to toggle source

Public: Lazily loads instances

# File lib/ec2/EC2.rb, line 337
def instances
  @instances ||= init_instances.reject(&:terminated?)
end
named_instances() click to toggle source

Public

Returns a Hash of instance name or id to Aws::EC2::Types::Instance

# File lib/ec2/EC2.rb, line 314
def named_instances
  @named_instances ||= Hash[instances.map { |i| [i.name || i.instance_id, i] }]
end
named_network_acls() click to toggle source

Public

Returns a Hash of network acl name or id to Aws::EC2::Types::NetworkAcl

# File lib/ec2/EC2.rb, line 167
def named_network_acls
  @named_network_acls = Hash[network_acls.map do |acl|
    [acl.name || acl.network_acl_id, acl]
  end]
end
named_network_interfaces() click to toggle source

Public

Returns a Hash of interface name to Aws::EC2::Types::NetworkInterface

# File lib/ec2/EC2.rb, line 234
def named_network_interfaces
  @named_network_interfaces ||= Hash[network_interfaces.map { |net| [net.name || net.network_interface_id, net] }]
    .reject { |k, v| !k or !v }
end
named_placement_groups() click to toggle source

Public

Returns a Hash of placement group name to Aws::EC2::Types::PlacementGroup

# File lib/ec2/EC2.rb, line 344
def named_placement_groups
  @named_placement_groups ||= Hash[placement_groups.map { |pg| [pg.group_name, pg] }]
end
named_route_tables() click to toggle source

Public

Returns a Hash of route tables name or id to Aws::EC2::Types::RouteTable

# File lib/ec2/EC2.rb, line 105
def named_route_tables
  @named_route_tables ||= Hash[route_tables.map { |rt| [rt.name || rt.route_table_id, rt] }]
    .reject { |k, v| !k or !v }
end
named_subnets() click to toggle source

Public

Returns a Hash of subnet name or id to Aws::EC2::Types::Subnet

# File lib/ec2/EC2.rb, line 58
def named_subnets
  @named_subnets ||= Hash[subnets.map { |subnet| [subnet.name || subnet.subnet_id, subnet] }]
    .reject { |k, v| !k or !v }
end
named_vpcs() click to toggle source

Public

Returns a Hash of VPC name or id to Aws::EC2::Types::Vpc

# File lib/ec2/EC2.rb, line 80
def named_vpcs
  @named_vpcs ||= Hash[vpcs.map { |vpc| [vpc.name || vpc.vpc_id, vpc] }]
    .reject { |k, v| !k or !v }
end
network_acls() click to toggle source

Public: Lazily load the network acls

# File lib/ec2/EC2.rb, line 174
def network_acls
  @network_acls ||= init_network_acls
end
network_interfaces() click to toggle source

Public: Lazily load network interfaces

# File lib/ec2/EC2.rb, line 256
def network_interfaces
  @network_interfaces ||= init_network_interfaces
end
placement_groups() click to toggle source

Public: Lazily load placement groups

# File lib/ec2/EC2.rb, line 349
def placement_groups
  @placement_groups ||= init_placement_groups
end
public_addresses() click to toggle source

Public

Returns a Hash of public ip to Aws::EC2::Types::Address

# File lib/ec2/EC2.rb, line 212
def public_addresses
  @public_addresses ||= Hash[addresses.map { |addr| [addr.public_ip, addr] }]
end
refresh_network_acls!() click to toggle source

Public: Refresh the list of Network ACLs

# File lib/ec2/EC2.rb, line 179
def refresh_network_acls!
  @network_acls = init_network_acls
end
refresh_route_tables!() click to toggle source

Public refreshes the list of route tables

# File lib/ec2/EC2.rb, line 141
def refresh_route_tables!
  @route_tables = init_route_tables
end
refresh_vpcs!() click to toggle source

Public refreshes the list of vpcs

# File lib/ec2/EC2.rb, line 93
def refresh_vpcs!
  @vpcs = init_vpcs
end
route_tables() click to toggle source

Public: Lazily load route tables

# File lib/ec2/EC2.rb, line 136
def route_tables
  @route_tables ||= init_route_tables
end
subnet_network_acls() click to toggle source

Public

Returns a Hash of subnet id to Aws::EC2::Types::NetworkAcl associated with the subnet

# File lib/ec2/EC2.rb, line 148
def subnet_network_acls
  @subnet_network_acls =
  Hash[network_acls.flat_map do |acl|
    acl.subnet_ids.map { |subnet_id| [subnet_id, acl] }
  end]
end
subnet_route_tables() click to toggle source

Public

Returns a Hash of subnet id to Aws::EC2::Types::RouteTable

# File lib/ec2/EC2.rb, line 113
def subnet_route_tables
  @subnet_route_tables ||= Hash[route_tables.flat_map do |rt|
    rt.subnet_ids.map { |subnet_id| [subnet_id, rt] }
  end]
end
subnets() click to toggle source

Public: Lazily load the subnets

# File lib/ec2/EC2.rb, line 73
def subnets
  @subnets ||= init_subnets
end
supported_platforms() click to toggle source
# File lib/ec2/EC2.rb, line 390
def supported_platforms
  @supported_platforms ||= @@client.describe_account_attributes({
    attribute_names: ["supported-platforms"]
  }).account_attributes.first.attribute_values.map(&:attribute_value)
end
supports_ec2_classic() click to toggle source
# File lib/ec2/EC2.rb, line 386
def supports_ec2_classic
  @supports_ec2_classic ||= supported_platforms.include?("EC2")
end
supports_vpc() click to toggle source
# File lib/ec2/EC2.rb, line 382
def supports_vpc
  @supports_vpc ||= supported_platforms.include?("VPC")
end
vpc_addresses() click to toggle source

Public

Returns a Hash of vpc id to array of Aws::EC2::Types::Address that has a network interface in the vpc

# File lib/ec2/EC2.rb, line 219
def vpc_addresses
  @vpc_addresses ||= Hash[id_vpcs.map do |vpc_id, _|
    interface_ids = vpc_network_interfaces[vpc_id].map { |interface| interface.network_interface_id }
    [vpc_id, addresses.select { |addr| interface_ids.include? addr.network_interface_id }]
  end]
end
vpc_endpoints() click to toggle source

Public: Lazily load the vpc endpoints

Returns a Hash of vpc id to array of Aws::EC2::Types::VpcEndpoint

# File lib/ec2/EC2.rb, line 198
def vpc_endpoints
  @vpc_endpoints ||= Hash[id_vpcs.map do |vpc_id, _|
    [vpc_id, endpoints.select { |e| e.vpc_id == vpc_id } ]
  end]
end
vpc_network_acls() click to toggle source

Public

Returns a Hash of vpc id to array of Aws::EC2::Types::NetworkAcl

# File lib/ec2/EC2.rb, line 158
def vpc_network_acls
  @vpc_network_acls = Hash[id_vpcs.map do |vpc_id, _|
    [vpc_id, network_acls.select { |acl| acl.vpc_id == vpc_id }]
  end]
end
vpc_network_interfaces() click to toggle source

Public

Returns a Hash of vpc id to array of Aws::EC2::Types::NetworkInterface

# File lib/ec2/EC2.rb, line 249
def vpc_network_interfaces
  @vpc_network_interfaces ||= Hash[id_vpcs.map do |vpc_id, _|
    [vpc_id, network_interfaces.select { |net| net.vpc_id == vpc_id}]
  end]
end
vpc_route_tables() click to toggle source

Public

Returns a Hash of vpc id to array of Aws::EC2::Types::RouteTable

# File lib/ec2/EC2.rb, line 122
def vpc_route_tables
  @vpc_route_tables ||= Hash[id_vpcs.map do |vpc_id, _|
    [vpc_id, route_tables.select { |rt| rt.vpc_id == vpc_id }]
  end]
end
vpc_subnets() click to toggle source

Public

Returns a Hash of VPC id to array of Aws::EC2::Types::Subnet for the VPC

# File lib/ec2/EC2.rb, line 66
def vpc_subnets
  @vpc_subnets ||= Hash[id_vpcs.map do |vpc_id, _|
    [vpc_id, subnets.select { |subnet| subnet.vpc_id == vpc_id }]
  end]
end
vpcs() click to toggle source

Public: Lazily load the vpcs

# File lib/ec2/EC2.rb, line 98
def vpcs
  @vpcs ||= init_vpcs
end

Private Class Methods

init_addresses() click to toggle source

Internal: Load allocated addresses

Returns the address as Aws::EC2::Types::Address

# File lib/ec2/EC2.rb, line 456
def init_addresses
  @@client.describe_addresses.addresses
end
init_dhcp_options() click to toggle source

Internal: Load DHCP Options

Returns the dhcp options as Aws::EC2::Types::DhcpOptions

# File lib/ec2/EC2.rb, line 429
def init_dhcp_options
  @@client.describe_dhcp_options.dhcp_options
end
init_ebs_volumes() click to toggle source

Internal: Load EBS Volumes

Returns the volumes as Aws::EC2::Types::Volume

# File lib/ec2/EC2.rb, line 470
def init_ebs_volumes
  @@client.describe_volumes.volumes
end
init_endpoints() click to toggle source

Internal: Load VPC Endpoints

Returns the vpc endpoints as Aws::EC2::Types::VpcEndpoint

# File lib/ec2/EC2.rb, line 436
def init_endpoints
  endpoints = []
  next_token = nil
  all_records_retrieved = false

  until all_records_retrieved
    response = @@client.describe_vpc_endpoints({
      next_token: next_token
    })
    next_token = response.next_token
    all_records_retrieved = next_token.nil? || next_token.empty?
    endpoints << response.vpc_endpoints
  end

  endpoints.flatten
end
init_instances() click to toggle source

Internal: Load instances

Returns the instances as Aws::EC2::Types::Instance

# File lib/ec2/EC2.rb, line 477
def init_instances
  instances = []
  next_token = nil
  all_records_retrieved = false

  until all_records_retrieved
    response = @@client.describe_instances({
      next_token: next_token
    })
    next_token = response.next_token
    all_records_retrieved = next_token.nil? || next_token.empty?
    instances << response.reservations.map { |r| r.instances }
  end

  instances.flatten
end
init_network_acls() click to toggle source

Internal: Load network acls

Returns the network acls as Aws::EC2::Types::NetworkAcl

# File lib/ec2/EC2.rb, line 422
def init_network_acls
  @@client.describe_network_acls.network_acls
end
init_network_interfaces() click to toggle source

Internal: Load network interfaces

Returns the network interface as Aws::EC2::Types::NetworkInterface

# File lib/ec2/EC2.rb, line 463
def init_network_interfaces
  @@client.describe_network_interfaces.network_interfaces
end
init_placement_groups() click to toggle source

Internal: Load placement groups

Returns the placement groups as Aws::EC2::Types::PlacementGroup

# File lib/ec2/EC2.rb, line 497
def init_placement_groups
  @@client.describe_placement_groups.placement_groups
end
init_route_tables() click to toggle source

Internal: Load route tables

Returns the route tables as Aws::EC2::Types::RouteTable

# File lib/ec2/EC2.rb, line 415
def init_route_tables
  @@client.describe_route_tables.route_tables
end
init_subnets() click to toggle source

Internal: Load all subnets

Returns an array of Aws::EC2::Types::Subnet

# File lib/ec2/EC2.rb, line 401
def init_subnets
  @@client.describe_subnets.subnets
end
init_vpcs() click to toggle source

Internal: Load VPCs

Returns the VPCs as Aws::EC2::Types::Vpc

# File lib/ec2/EC2.rb, line 408
def init_vpcs
  @@client.describe_vpcs.vpcs
end