Skip to content

Instantly share code, notes, and snippets.

@ekivemark
Created September 24, 2019 20:11
Show Gist options
  • Save ekivemark/bef78952fa362e67ac686bfcf343a823 to your computer and use it in GitHub Desktop.
Save ekivemark/bef78952fa362e67ac686bfcf343a823 to your computer and use it in GitHub Desktop.
Parameterize wsgi.py
I recommend modfying wsgi.py for VMI/SMH/SMH_APP to enable some flexibility in the call to the EC2 parameter store.
THe current code is:
###########################################
import os
from .ssmenv import EC2ParameterStore
from django.core.wsgi import get_wsgi_application
try:
parameter_store = EC2ParameterStore(region_name="us-east-1")
# Automate env (dev)
django_parameters = parameter_store.get_parameters_by_path(
'/dev/sharemyhealth/',
strip_path=True)
EC2ParameterStore.set_env(django_parameters)
except Exception:
pass
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sharemyhealth.settings')
application = get_wsgi_application()
###########################################
proposed revision:
###########################################
import os
from getenv import env
from .ssmenv import EC2ParameterStore
from django.core.wsgi import get_wsgi_application
AWS_DEFAULT_REGION = env('AWS_DEFAULT_REGION', 'us-east-1')
VPC_ENV = env('VPC_ENV', 'dev')
VPC_APP_NAME = env('VPC_APP_NAME', 'sharemyhealth')
PARAMETER_STORE_PATH = "/%s/%s/" % (VPC_ENV, VPC_APP_NAME)
# e.g. /staging/vmi, /prod/sharemyhealth, /dev/smh_app
try:
parameter_store = EC2ParameterStore(region_name=AWS_DEFAULT_REGION)
# Automate env (dev)
django_parameters = parameter_store.get_parameters_by_path(
PARAMETER_STORE_PATH,
strip_path=True)
EC2ParameterStore.set_env(django_parameters)
except Exception:
pass
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sharemyhealth.settings')
application = get_wsgi_application()
###########################################
This would allow a couple of environment variables to enable pulling of parameterstore variables based upon
vpc environment and app name. Once code is stable it provides potential to change variables without
rebuilding machines. Machine builds would then be limited to patching and code updates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment