I'm using Django and I have the ALLOWED_HOSTS setting to include my EC2's private IP as per below:
import requests
EC2_PRIVATE_IP = None
try:
EC2_PRIVATE_IP = requests.get('http://169.254.169.254/latest/meta-data/local-ipv4', timeout=0.01).text
except requests.exceptions.RequestException:
pass
if EC2_PRIVATE_IP and not DEBUG:
ALLOWED_HOSTS.append(EC2_PRIVATE_IP)
Problem is that the above does not take into consideration the ELB's that forward the request to my EC2 instances. Is there a way to make that work programmatically? Can I request the public IP address or have setting to check the DNS instead?
I'm seeing this issue with the ELB's public IP address.