We\'ve recently changed our deployment strategy to use AWS auto scaling group.
One problem we have in production is with the newly created EC2s.
Our Application sta
The snippets below will find either the Public IP address or the Elastic IP address associated with your EC2 instance and append it to ALLOWED_HOSTS.
Install PyCurl
pip install pycurl
Python 3
import pycurl
from io import BytesIO
# Determine Public IP address of EC2 instance
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'checkip.amazonaws.com')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
# Body is a byte string, encoded. Decode it first.
ALLOWED_HOSTS.append(buffer.getvalue().decode('iso-8859-1').strip())
Python 2
import pycurl
from StringIO import StringIO
buffer = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'checkip.amazonaws.com')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
# In Python 2, we can cast the return value to
# string without knowing the exact encoding.
ALLOWED_HOSTS.append(str(buffer.getvalue()).strip())