user1788294
user1788294

Reputation: 1893

Know its own instance id using aws-sdk ruby gem

How does a running ec2 instance can know its own instance id using aws-sdk ruby gem.

I have a running ec2 instance say 'X' and I want to know its instance id using aws-sdk ruby gem. The ruby code is getting executed on the same ec2 instance 'X'

Upvotes: 5

Views: 2452

Answers (1)

Mike H-R
Mike H-R

Reputation: 7835

There are a bunch of solutions over here

A ruby one looks like this:

require 'rubygems'
require 'aws-sdk'
require 'net/http'

metadata_endpoint = 'http://169.254.169.254/latest/meta-data/'
instance_id = Net::HTTP.get( URI.parse( metadata_endpoint + 'instance-id' ) )

ec2 = AWS::EC2.new()
instance = ec2.instances[instance_id]

Upvotes: 7

Related Questions