How to check application runs in AWS EC2 instance

前端 未结 4 444
别那么骄傲
别那么骄傲 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条回答
  •  猫巷女王i
    2021-01-11 11:56

    The better way to do this would be to make a request to get instance metadata.

    From the AWS Documentation:

    To view all categories of instance metadata from within a running instance, use the following URI:

    http://169.254.169.254/latest/meta-data/

    On a Linux instance, you can use a tool such as cURL, or use the GET command, for example:

    PROMPT> GET http://169.254.169.254/latest/meta-data/

    Here's an example using the Python Boto wrapper:

    from boto.utils import get_instance_metadata
    
    m = get_instance_metadata()
    
    if len(m.keys()) > 0:
        print "Running on EC2"
    
    else:
        print "Not running on EC2"
    

提交回复
热议问题