How to check application runs in AWS EC2 instance

无人久伴 提交于 2019-12-19 05:22:48

问题


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 Azure role instance
}
else if(isAWS())
{
   //run in AWS EC2 instance
}
else
{
   //run in the non-cloud system
}

//checked whether it runs in AWS EC2 instance or not.
bool isAWS()
{
  string url = "http://instance-data";
  try
  {
     WebRequest req = WebRequest.Create(url);
     req.GetResponse();
     return true;
  }
  catch
  {
     return false;
  }  
}

but I have one problem when my apps runs in the non-cloud system, like local windows system. It got very slowly while executing isAWS() method. the code 'req.GetResponse()' takes a long time. so I want to know how can I to deal with it? please help me! thanks in advance.


回答1:


As you said the WebRequest.Create() call is slow on your desktop so you really need to check the network traffic (using Netmon) to actually determine what took long time. This request, opens connection, connects to target server, downloads the content and then close the connection so it is good to know where this time is taken.

Also if you just want to know if any URL (on Azure, on EC2 or any other web server is live and working fine you can just request to only download headers by using

string URI = "http://www.microsoft.com";
HttpWebRequest  req = (HttpWebRequest)WebRequest.Create(URI);
req.Method = WebRequestMethods.Http.Head;
var response = req.GetResponse();
int TotalSize = Int32.Parse(response.Headers["Content-Length"]);
// Now you can parse the headers for 200 OK and know that it is working.

You can also use GET only a range of the data instead of full data to expedite to call:

HttpWebRequest myHttpWebReq =(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebReq.AddRange(-200, ContentLength); // return first 0-200 bytes
//Now you can send the request and then parse date for headers for 200 OK

Any of the above method will be faster to get where your site is running.




回答2:


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"



回答3:


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



回答4:


On ec2 Ubuntu instances, the file /sys/hypervisor/uuid exists and its first three characters are 'ec2'. I like using this because it doesn't rely on external servers.



来源:https://stackoverflow.com/questions/10907418/how-to-check-application-runs-in-aws-ec2-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!