Skip to content

Instantly share code, notes, and snippets.

@dryan
Last active September 7, 2023 11:04
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dryan/8271687 to your computer and use it in GitHub Desktop.
Save dryan/8271687 to your computer and use it in GitHub Desktop.
Handling EC2 ELB health checks and Django's ALLOWED_HOSTS setting.
# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = [
'yourdomain.tld',
'.compute-1.amazonaws.com', # allows viewing of instances directly
]
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:
ALLOWED_HOSTS.append(EC2_PRIVATE_IP)
@souravjamwal77
Copy link

Great,
I also have somewhat similar try/except block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment