Module: VScripts::AWS::Metadata

Included in:
EC2
Defined in:
lib/vscripts/aws/metadata.rb

Overview

This module contacts the AWS Metadata service to collect information about the EC2 instance.

Instance Method Summary collapse

Instance Method Details

#check_instanceObject

Fail if the current instance is not an EC2 instance



43
44
45
46
47
# File 'lib/vscripts/aws/metadata.rb', line 43

def check_instance
  unless ec2_instance?
    abort 'FATAL: NOT an EC2 instance or could not connect to Metadata'
  end
end

#ec2_instance?Boolean

Returns Check connection to service.

Returns:

  • (Boolean)

    Check connection to service



36
37
38
39
40
# File 'lib/vscripts/aws/metadata.rb', line 36

def ec2_instance?
  Net::HTTP.get_response(URI.parse()) && true
rescue
  false
end

#instance_idString

Returns Get instance’s ID from metadata.

Returns:

  • (String)

    Get instance’s ID from metadata



26
27
28
# File 'lib/vscripts/aws/metadata.rb', line 26

def instance_id
  open("#{}/instance-id").read
end

#metadata_urlString

Returns Metadata URL.

Returns:

  • (String)

    Metadata URL



10
11
12
# File 'lib/vscripts/aws/metadata.rb', line 10

def 
  'http://instance-data/latest/meta-data'
end

#public_hostnameString

Returns Get instance’s public hostname from metadata.

Returns:

  • (String)

    Get instance’s public hostname from metadata



31
32
33
# File 'lib/vscripts/aws/metadata.rb', line 31

def public_hostname
  open("#{}/public-hostname").read
end

#regionString

Returns Get the region from the zone.

Returns:

  • (String)

    Get the region from the zone



21
22
23
# File 'lib/vscripts/aws/metadata.rb', line 21

def region
  zone[/^(.*[\d])[a-z]$/, 1]
end

#zoneString

Returns Get availability zone from metadata.

Returns:

  • (String)

    Get availability zone from metadata



15
16
17
18
# File 'lib/vscripts/aws/metadata.rb', line 15

def zone
  open("#{}/placement/availability-zone")
    .read
end