class AwsEc2DnsName

Constants

Instance

Attributes

client[RW]

Public Class Methods

new(region: nil, access_key_id: nil, secret_access_key: nil) click to toggle source

@param [String] region @param [String] access_key_id @param [String] secret_access_key

# File lib/aws_ec2_dns_name.rb, line 13
def initialize(region: nil, access_key_id: nil, secret_access_key: nil)
  self.client = Aws::EC2::Client.new(region: region,
                                     access_key_id: access_key_id,
                                     secret_access_key: secret_access_key)
end

Public Instance Methods

instances() click to toggle source

@return [Array<AwsEc2DnsName:Instance>]

# File lib/aws_ec2_dns_name.rb, line 20
def instances
  client.describe_instances.first.reservations.map do |reservation|
    ec2_instance = reservation.instances.first
    name_tag = ec2_instance.tags.find { |tag| tag.key == "Name" }
    next unless name_tag
    name_tag_value = name_tag.value
    dns_name = dns_name(ec2_instance)

    # dns_name of terminated instance is empty string
    next if dns_name == ""

    AwsEc2DnsName::Instance.new(name_tag_value, dns_name)
  end.compact.sort_by { |i| i[:name_tag] }
end
Also aliased as: list
list()
Alias for: instances

Private Instance Methods

dns_name(ec2_instance) click to toggle source

@param [Aws::EC2::Types::Instance] ec2_instance @return [String]

# File lib/aws_ec2_dns_name.rb, line 40
def dns_name(ec2_instance)
  public_dns_name = ec2_instance.public_dns_name
  private_dns_name = ec2_instance.private_dns_name

  if public_dns_name.empty?
    private_dns_name
  else
    public_dns_name
  end
end