簡體   English   中英

如何判斷我的機器是否為EC2實例?

[英]How can I tell if my machine is an EC2 instance or not?

我需要一個Ruby函數,即使在我們的EC2實例上DNS斷開時,它也會100%地告訴我這台機器是否為EC2實例。

我們使用的功能是:

def is_ec2?
  require 'socket'
  Socket::gethostbyname('instance-data.ec2.internal.')
  true
rescue
  false
end

除了DNS斷開時,每台EC2機器都認為它不是EC2機器,並且發生了壞事,例如生產機器刪除了自己的SSL證書,並用本地開發箱的證書替換了它們。

在Python中,我們使用:

@memoized
def is_ec2():
    # This is a 99% check to avoid the need to wait for the timeout. 
    # Our VM's have this file.  Our dev VM's don't.  
    if not os.path.isfile('/sys/hypervisor/uuid'):
        return False

    try:
        result = boto.utils.get_instance_metadata()
        if result == {}:
            return False
        return True
    except Exception:
        return False

除了使用wget -q -O - http://169.254.169.254/latest/meta-data : boto.utils.get_instance_metadata()代替boto.utils.get_instance_metadata()boto.utils.get_instance_metadata()嗎?

只要將其放在您的主機文件中,您就不必擔心DNS。

但是,實際上,使用ENV變量不是更有意義嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM