Boto get_instance_metadata Hangs When Not on EC2

The Symptom

The following script hangs if ran on a machine which isn’t an EC2 instance:

#!/usr/bin/env python
from boto.utils import get_instance_metadata
get_instance_metadata()

Why Does it Hang?

get_instance_metadata makes a HTTP call to the instance metadata service. This service is inaccessible outside of AWS so the socket library hangs trying to make the connection. Normally you would expect this connection to timeout, but by default the timeout is set to None so it will wait forever.

How to Fix It

The fix is simply to reduce the timeout:

#!/usr/bin/env python
from boto.utils import get_instance_metadata
get_instance_metadata(timeout=2, num_retries=2)

Notes: