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
-
#check_instance ⇒ Object
Fail if the current instance is not an EC2 instance.
-
#ec2_instance? ⇒ Boolean
Check connection to service.
-
#instance_id ⇒ String
Get instance’s ID from metadata.
-
#metadata_url ⇒ String
Metadata URL.
-
#public_hostname ⇒ String
Get instance’s public hostname from metadata.
-
#region ⇒ String
Get the region from the zone.
-
#zone ⇒ String
Get availability zone from metadata.
Instance Method Details
#check_instance ⇒ Object
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.
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_id ⇒ String
Returns 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_url ⇒ String
Returns 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 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 |
#region ⇒ String
Returns 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 |
#zone ⇒ String
Returns 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 |