Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS beanstalk + Django: 502 Bad Gateway - ModuleNotFoundError: No module named 'application'

I'm trying to deploy Django project to Elastic Beanstalk. I'm following their guide: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html But after eb open all pages return "502 Bad Gateway".

My AWS admin shows django environment as "Degraded". Please see the screenshot: enter image description here

My logs, seems like something with "ModuleNotFoundError: No module named 'application'":

----------------------------------------
/var/log/web.stdout.log
----------------------------------------
Jan 27 19:30:21 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
Jan 27 19:30:21 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
Jan 27 19:30:21 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
Jan 27 19:30:21 ip-172-31-32-137 web: ModuleNotFoundError: No module named 'application'
Jan 27 19:30:21 ip-172-31-32-137 web: [2021-01-27 19:30:21 +0000] [3483] [INFO] Worker exiting (pid: 3483)
Jan 27 19:30:21 ip-172-31-32-137 web: [2021-01-27 19:30:21 +0000] [3476] [INFO] Shutting down: Master
Jan 27 19:30:21 ip-172-31-32-137 web: [2021-01-27 19:30:21 +0000] [3476] [INFO] Reason: Worker failed to boot.
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3488] [INFO] Starting gunicorn 20.0.4
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3488] [INFO] Listening at: http://127.0.0.1:8000 (3488)
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3488] [INFO] Using worker: threads
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3495] [INFO] Booting worker with pid: 3495
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3495] [ERROR] Exception in worker process
Jan 27 19:30:22 ip-172-31-32-137 web: Traceback (most recent call last):
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
Jan 27 19:30:22 ip-172-31-32-137 web: worker.init_process()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
Jan 27 19:30:22 ip-172-31-32-137 web: super().init_process()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process
Jan 27 19:30:22 ip-172-31-32-137 web: self.load_wsgi()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
Jan 27 19:30:22 ip-172-31-32-137 web: self.wsgi = self.app.wsgi()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
Jan 27 19:30:22 ip-172-31-32-137 web: self.callable = self.load()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
Jan 27 19:30:22 ip-172-31-32-137 web: return self.load_wsgiapp()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
Jan 27 19:30:22 ip-172-31-32-137 web: return util.import_app(self.app_uri)
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
Jan 27 19:30:22 ip-172-31-32-137 web: mod = importlib.import_module(module)
Jan 27 19:30:22 ip-172-31-32-137 web: File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
Jan 27 19:30:22 ip-172-31-32-137 web: return _bootstrap._gcd_import(name[level:], package, level)
Jan 27 19:30:22 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
Jan 27 19:30:22 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
Jan 27 19:30:22 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
Jan 27 19:30:22 ip-172-31-32-137 web: ModuleNotFoundError: No module named 'application'

I guess there might be problem with wsgi configuration.

django.config

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: todo.wsgi

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo.settings')

application = get_wsgi_application()

config.yaml

branch-defaults:
  main:
    environment: django-env
    group_suffix: null
global:
  application_name: todo
  branch: null
  default_ec2_keyname: null
  default_platform: Python 3.7 running on 64bit Amazon Linux 2
  default_region: eu-north-1
  include_git_submodules: true
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: eb-cli
  repository: null
  sc: git
  workspace_type: Application

Would really appreciate some help!

like image 857
NatalieLes Avatar asked Nov 15 '22 00:11

NatalieLes


1 Answers

Django configurations are different according to to the Amazon Linux version you prefer to deploy.

If you are deploying your application to Amazon Linux 2,

.ebextensions/django.conf

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: todo.wsgi:application
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: todo.settings
  "aws:elasticbeanstalk:environment:proxy:staticfiles":
    "/static/": "todo/static/"

If you are deploying your application to old version:

.ebextensions/django.conf

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: todo/wsgi
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: todo.settings
  "aws:elasticbeanstalk:environment:proxy:staticfiles":
    "/static/": "todo/static/"

BTW you may want to check the WSGIPath in elastic beanstalk configuration (Container Options) using AWS console:

EBS Container Options

like image 108
Arda Taşcı Avatar answered Dec 21 '22 20:12

Arda Taşcı