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
-
#check_instance ⇒ Object
Fails if the current instance is not an EC2 instance.
-
#ec2_instance? ⇒ Boolean
Checks connection to metadata service.
-
#instance_id ⇒ String
The instance ID.
-
#metadata_url ⇒ String
The Metadata URL.
-
#public_hostname ⇒ String
The instance public hostname.
-
#region ⇒ String
The region.
-
#zone ⇒ String
The availability zone.
Instance Method Details
#check_instance ⇒ Object
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
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_id ⇒ String
Returns the instance ID.
26 27 28 |
# File 'lib/vscripts/aws/metadata.rb', line 26 def instance_id open("#{}/instance-id").read end |
#metadata_url ⇒ String
Returns the Metadata URL.
10 11 12 |
# File 'lib/vscripts/aws/metadata.rb', line 10 def 'http://instance-data/latest/meta-data' end |
#public_hostname ⇒ String
Returns the instance public hostname.
31 32 33 |
# File 'lib/vscripts/aws/metadata.rb', line 31 def public_hostname open("#{}/public-hostname").read end |
#region ⇒ String
Returns the region.
21 22 23 |
# File 'lib/vscripts/aws/metadata.rb', line 21 def region zone[/^(.*[\d])[a-z]$/, 1] end |
#zone ⇒ String
Returns the availability zone.
15 16 17 18 |
# File 'lib/vscripts/aws/metadata.rb', line 15 def zone open("#{}/placement/availability-zone") .read end |