How to check application runs in AWS EC2 instance

前端 未结 4 429
别那么骄傲
别那么骄傲 2021-01-11 11:11

How can I check which platform my app runs, AWS EC2 instance, Azure Role instance and non-cloud system? now I do that like this:

if(isAzure())
{
    //run in         


        
4条回答
  •  没有蜡笔的小新
    2021-01-11 12:08

    I think your original idea is pretty good, but no need to make the web request. Simply try to see if the name resolves (in python):

    def is_ec2():
        import socket
        try:
            socket.gethostbyname('instance-data.ec2.internal.')
            return True
        except socket.gaierror:
            return False
    

提交回复
热议问题