Know its own instance id using aws-sdk ruby gem

amazon-ec2, amazon-web-services, ruby

Solution

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]

Problem

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'

Original source

Related problems