eazynow
eazynow

Reputation: 23

How can I check with boto if a python (django) app is running in EC2?

I currently have a django app that is deployed to EC2. I was going to add some extra logging info in using boto.utils to get things like the instance id. However when running the code locally, the call to boto.utils.get_instance_metadata()['instance-id'] just hangs, rather than return None or an empty string.

I can't seem to see in boto if there is a flag or function to check if you are on an EC2.

Does anyone know of any?

Thanks!

Upvotes: 1

Views: 300

Answers (1)

garnaat
garnaat

Reputation: 45846

Checking for the existence of the metadata server is the best (only?) way I know of to detect if you are running on an EC2 instance or not. The get_instance_metadata function takes a couple of optional parameters that you can supply to control the timeouts and retry strategy. For example:

>>> boto.utils.get_instance_metadata(timeout=1, num_retries=1)
{}
>>>

Will use a timeout of one second and will retry one time. You could also specify num_retries of zero if you want the call to return even faster. Note however that you do occasionally get failed requests on an actual EC2 instance so having at least one retry would be safer.

Upvotes: 1

Related Questions