5

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'

user1788294
  • 1,823
  • 4
  • 24
  • 30

1 Answers1

7

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]
Community
  • 1
  • 1
Mike H-R
  • 7,726
  • 5
  • 43
  • 65