Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I would like to run some scripts on hosts which are EC2 instances but I don't know how to be sure that the host is really an EC2 instance.

I have made some tests, but this is not sufficient:

  • Test that binary ec2_userdata is available (but this will not always be true)
  • Test availability of "http://169.254.169.254/latest/meta-data" (but will this be always true ? and what is this "magical IP" ?)
share|improve this question
    
    
It's an APIPA address actually, which is quite odd to use as a reference for a critical service like meta data retrieval. –  Matthieu Cerda Jan 4 '13 at 9:52
1  
The IP ranges of EC2s are public (though varying from time to time). If you keep up with a current list you can check the instances IP against that ranges. –  Karma Fusebox Jan 4 '13 at 10:30
1  
Don't rely on 169.254.169.254 if you want EC2 and only EC2 - EC2-alike systems like Eucalyptus also support it. engage.eucalyptus.com/customer/portal/articles/… –  ceejayoz Jul 2 '13 at 14:21
add comment

4 Answers 4

up vote 2 down vote accepted

Well actually, there is a very simple way to detect if the host is an EC2 instance: check the reverse lookup of your public IP. The EC2 reverses are quite hard to miss.

Also, if you did not modify it, the hostname should be your reverse, making it further easy to spot it.

You might also use the "magical IP" you talked about, as it is indeed the standard way to get EC2 Instance tags, however, if you are not on a EC2 network, you will have to wait for a timeout, which is generally not desirable...

If these methods are not enough, just do a whois of your IP and check if you are within and Amazon EC2 IP block.

EDIT: You may use this small shell bit:

#!/bin/bash
LOCAL_HOSTNAME=$(hostname -d)
if [[ ${LOCAL_HOSTNAME} =~ .*\.amazonaws\.com ]]
then
        echo "This is an EC2 instance"
else
        echo "This is not an EC2 instance, or a reverse-customized one"
fi

Careful though, [[ is a bashism. You may also use a Python or Perl uniline, YMMV.

share|improve this answer
3  
this doesn't work in a VPC or an environment where you have changed the hostname; eg. if your machines are in domain.local –  Preflightsiren Jan 28 '13 at 9:28
    
the hostname bit is just bound to fail. –  Dan Pritts Feb 13 at 19:28
add comment

Look for the metadata by the EC2 internal domain name instead of IP, which will return a fast DNS failure if you're not on EC2 and avoids IP conflicts or routing issues:

curl http://instance-data.ec2.internal; echo $?

share|improve this answer
add comment

Perhaps you can use "facter":

"Facter is a cross-platform library for retrieving simple operating system facts, like operating system, linux distribution, or MAC address."

http://www.puppetlabs.com/puppet/related-projects/facter/

For example, if we take a look to the ec2 fact (facter-1.6.12/lib/facter/ec2.rb):

require 'facter/util/ec2'
require 'open-uri'

def metadata(id = "")
  open("http://169.254.169.254/2008-02-01/meta-data/#{id||=''}").read.
    split("\n").each do |o|
    key = "#{id}#{o.gsub(/\=.*$/, '/')}"
    if key[-1..-1] != '/'
      value = open("http://169.254.169.254/2008-02-01/meta-data/#{key}").read.
        split("\n")
      symbol = "ec2_#{key.gsub(/\-|\//, '_')}".to_sym
      Facter.add(symbol) { setcode { value.join(',') } }
    else
      metadata(key)
    end
  end
end

def userdata()
  begin
    value = open("http://169.254.169.254/2008-02-01/user-data/").read.split
    Facter.add(:ec2_userdata) { setcode { value } }
  rescue OpenURI::HTTPError
  end
end

if (Facter::Util::EC2.has_euca_mac? || Facter::Util::EC2.has_openstack_mac? ||
    Facter::Util::EC2.has_ec2_arp?) && Facter::Util::EC2.can_connect?

  metadata
  userdata
else
  Facter.debug "Not an EC2 host"
end
share|improve this answer
add comment

Hostnames are likely to change, run a whois against your public IP:

if [[ ! -z $(whois $(curl -s shtuff.it/myip/short) | grep -i amazon) ]]; then 
  echo "I'm Amazon"
else 
  echo "I'm not Amazon"
fi

or hit the AWS meta-data url

if [[ ! -z $(curl -s http://169.254.169.254/1.0/) ]]; then 
  echo "I'm Amazon"
else 
  echo "I'm not Amazon"
fi
share|improve this answer
1  
Add a --connect-timeout 1 to the second curl statement to fail quickly if you're not running on EC2. –  Jonathan Oliver Sep 25 '13 at 12:27
    
FWIW, using the metadata URL can indicate it's running as a cloud instance, but cannot conclusively determine if it is specifically EC2. OpenStack and Eucalyptus also use the same metadata URI. I know this is picking nits, but for my work, which cloud provider matters. –  EmmEff Feb 20 at 15:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.