Module: VScripts::AWS::Metadata

Included in:
VScripts::AWS
Defined in:
lib/vscripts/aws/metadata.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#check_instanceObject

Fails if the current instance is not an EC2 instance



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

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

#ec2_instance?Boolean

Checks connection to metadata service

Returns:

  • (Boolean)


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

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

#instance_idString

Returns the instance ID.

Returns:

  • (String)

    the instance ID



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

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

#metadata_urlString

Returns the Metadata URL.

Returns:

  • (String)

    the Metadata URL



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

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

#public_hostnameString

Returns the instance public hostname.

Returns:

  • (String)

    the instance public hostname



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

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

#regionString

Returns the region.

Returns:

  • (String)

    the region



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

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

#zoneString

Returns the availability zone.

Returns:

  • (String)

    the availability zone



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

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