Django deploy on AWS EB CLI - python

I'm trying to deploy a basic Django project on AWS using ElasticBean. I tried everything, but can't understand what mistake I have done. So I will summarise here all 'solutions' on the internet. Please help !
First of, this is my Django project:
.ebextensions
-django.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: ebdjango.wsgi:application
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: ebdjango.settings
.elasticbeantalk
branch-defaults:
default:
environment: eb-env
global:
application_name: ebdjango
branch: null
default_ec2_keyname: null
default_platform: Python 3.8
default_region: us-west-2
include_git_submodules: true
instance_profile: null
platform_name: null
platform_version: null
profile: eb-cli
repository: null
sc: null
workspace_type: Application
settings.py
ALLOWED_HOSTS = ['http://ebdjango-env.eba-b3pcnpc8.us-east-1.elasticbeanstalk.com/']
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ebdjango.settings')
application = get_wsgi_application()
requirements.txt
Django==3.2
gunicorn==20.1.0
There is nothing in my Django app, no templates, no modules. I created it to be able to upload it. I'm using Django 3.2 and Python 3.8
When loading the webpage (CNAME): 502 Bad Gateway / nginx/1.22.1
When looking the log file I see this error: web: ModuleNotFoundError: No module named 'ebdjango.wsgi'
The entire project is available here: https://github.com/mkchmura/AWS-EB-CLI.git

Related

502 occurs when creating environment on elastic beanstalk

I'm having an issue deploying my first Django project.
Here's my config.yml:
global:
application_name: testapp
branch: null
default_ec2_keyname: aws-eb
default_platform: Python 3.8 running on 64bit Amazon Linux 2
default_region: us-west-2
include_git_submodules: true
instance_profile: null
platform_name: null
platform_version: null
profile: eb-cli
repository: null
sc: null
workspace_type: Application
And here's my django.config:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: djangoproject.wsgi:application
I have followed this doc. But after I did eb create testapp-env, I get 502 error:
image of the error
I will provide further information if you need. Thank you in advance for your help.
Here's the error in web.stdout.log:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
UPDATE
My django project uses python-socketio, and here's my wsgi.py:
from django.core.wsgi import get_wsgi_application
import socketio
from post.socketioserver import sio # <- it's just my socket io code
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoproject.settings')
django_app = get_wsgi_application()
application = socketio.WSGIApp(sio, django_app)
I get another error:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
You need to set the DJANGO_SETTINGS_MODULE environment variable:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: djangoproject.wsgi:application
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: "djangoproject.settings"
Also you need to edit your wsgi.py file because you are accessing an app before Django setup:
import django
django.setup()
from django.core.wsgi import get_wsgi_application
import socketio
from post.socketioserver import sio # <- it's just my socket io code
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoproject.settings')
django_app = get_wsgi_application()
application = socketio.WSGIApp(sio, django_app)

Issue running Python37 on Google App Engine

I know this question has been asked in some facet or another, but I have gone through readings as shown here and I am still not seeing where my issue is as I am still unable to publish my Django 2.1.1 app in the Python37 environment in Google App Engine:
Python 3 Django on App Engine Standard: App Fails to Start
Overall what I am attempting to do is publish a simple app engine app using:
gcloud app deploy
My application works locally but when I publish, it goes through without issue, but I get the annoying:
500 Server Error message
When I look at the logs in Google I get the same error as many others have gotten:
ModuleNotFoundError: No module named 'main'
here is my relevant directory structure
project_portal
project_portal
init.py
settings.py
urls.py
wsgi.py
main.py
app.yaml
requirements.txt
my app.yaml file
runtime: python37
entrypoint: gunicorn -b :$PORT project_portal.wsgi
env: standard
handlers:
- url: .*
secure: always
redirect_http_response_code: 301
script: project_portal.wsgi.application
my project_portal/wsgi.py file
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_portal.settings')
application = get_wsgi_application()
from main.py in project root
from project_portal.wsgi import application
from requirements.txt
django == 2.1.1
Without an entrypoint defined, it will try to start from a file called main.py. Create one, at the same level as app.yamlwith some simple logic in it:
from project_portal.wsgi import application
Edit:
Since you have an entrypoint defined, it seems that is the issue. So, make sure you have gunicorn in your 'requirements.txt':
gunicorn==19.9.0
This did not help:
Try changing
entrypoint: gunicorn -b :$PORT project_portal.wsgi
to
entrypoint: gunicorn -b :$PORT project_portal.wsgi:application

Trouble deploying Django app to Heroku "no Cedar-supported app detected"

I've been struggling to deploy a small Django web app following the instructions given here. I've only been using sqlite3 to build my app in my development and everything works fine on the Django development server. When I try to deploy to Heroku, I get the error "Push rejected, no Cedar-supported app detected", but I think I have all the files required to get my app up and running. Been at it for days now with no success so I'll take any suggestions and help. Below is a sketch of my app, but feel free to sift the whole thing at my github repo.
landcrab/
landcrab/ <----- main project
settings/
__init__.py
base.py
local.py
production.py
__init__.py
urls.py
db.sqlite3
wsgi.py
vcrental/ <----- my app
admin.py
....
static/
....
.gitignore
db.sqlite3
manage.py
Procfile
requirements.txt
runtime.txt
In manage.py and wsgi.py I've set os.environ.setdefault("DJANGO_SETTINGS_MODULE", "landcrab.settings.production")
Procfile
web: gunicorn landcrab.wsgi --log-file -
requirements.txt (used pip freeze)
Django==1.7.1
dj-database-url==0.3.0
dj-static==0.0.6
django-toolbelt==0.0.1
gunicorn==19.1.1
jsmin==2.0.11
nose==1.3.4
psycopg2==2.5.4
pyparsing==2.0.3
python-dateutil==2.2
pytz==2014.9
six==1.8.0
static3==0.5.1
runtime.txt
python-3.4.2
For my settings file, I attempted to follow this structure
Production.py
from landcrab.settings.base import *
import dj_database_url
DEBUG = False
TEMPLATE_DEBUG = False
# Parse database configuration from $DATABASE_URL
DATABASES['default'] = dj_database_url.config()
# DATABASES['default'] = dj_database_url.config(default='postgres://user:pass#localhost/dbname')
# DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
# DATABASES = {'default': dj_database_url.config(default=os.environ.get('DATABASE_URL'))}
# DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
and finally wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "landcrab.settings.production") #Edited by me
from django.core.wsgi import get_wsgi_application
#Added by me for Heroku
try:
from dj_static import Cling
application = Cling(get_wsgi_application())
except:
application = get_wsgi_application()
Well this is embarrassing. I hadn't committed my changes before attempting to deploy to Heroku. After committing, I was able to deploy without error.

Django AWS Eslastic Beanktalk not deploying - Internal Server Error

I'm following this tutorial:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python_django.html
I'm on Step 6. I already created my AWS Elastic Beanstalk Python Application and now I want to link it to my Django application.
This is my directory:
~/Documents/myapp
myapp manage.py requirements.txt .ebextensions .elasticbeanstalk .git .gitignore
Inside my .ebextensions is myapp.config:
container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
option_settings:
- namespace: aws:elasticbeanstalk:container:python
option_name: WSGIPath
value: myapp/wsgi.py
- option_name: DJANGO_SETTINGS_MODULE
value: myapp.settings
- option_name: AWS_SECRET_KEY
value: myUsersSecretKey
- option_name: AWS_ACCESS_KEY_ID
value: myUsersAcessKey
Inside
~/Documents/myapp/myapp
is my settings.py, views.py etc. I made the database in my settings.py to:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
as it said in the tutorial. I then did
git add .
git commit -m "eb configuration"
git aws.push
it said that the environment update initiated successfully. After I waited for the status to be ready, I went to the URL and it is still the AWS Elastic Beanstalk Python success page and is not my django application. Any idea why?
Edit: I then tried following this tutorial:
https://www.youtube.com/watch?v=YJoOnKiSYws (pause at 7:44)
and changed my config file to:
container_commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only: true
option_settings:
- namespace: aws:elasticbeanstalk:container:python
option_name: WSGIPath
value: myapp/wsgi.py
- option_name: DJANGO_SETTINGS_MODULE
value: myapp.settings
- namespace: aws:autoscaling:launchconfiguration
option_name: EC2KeyName
value: myKeyPairName
and now when I refreshed the page after the status was ready, it gave an internal server error saying:
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the sever administrator, root#localhost and inform them of the time and error occured, and anything you might have done that may have acused the error.
More information about this error may be available in the server error log.
EDIT 2:
Is my wsgi.py file supposed to look a certain way when dealing with AWS elastic beanstalk? I left the wsgi.py file how it was when it was first created because the tutorial did as well. This is my wsgi.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ayflare.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I think there are a couple of things here,
You might want to try using "python manage.py syncdb --noinput" in
the config instead of using "django-admin.py". I seem to remember
encountering some issues until I switched to "python manage.py".
Another thing that might be worthwhile doing is enabling DEBUG in
your django settings file to see if you can get a more detailed error
message about the issue.
Sometimes, if you mess up the deployment is EB, rebuilding the
environment can help smooth out issues once you sort them in your
code/config files. You can do this in the Elastic Beanstalk page in
the AWS Management Console.
Your first myapp.config file looks correct. The updated one that you got from the youtube video doesn't seem correct if you are following the official Amazon Docs.

How to deploy structured Flask app on AWS elastic beanstalk

After successfully deploying a test app using the steps outlined here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python_flask.html
I tried to deploy my actual flask application which has the following structure:
myApp/
runServer.py
requirements.txt
myApp/
__init__.py
helpers.py
clean.sh
static/
myApp.css
handlers/
__init__.py
views.py
templates/
layout.html
viewOne.html
viewTwo.html
Where views.py contains my url mappings.
I have tried initializing the eb instance in the root directory as well as within the myApp module and git aws.push but I get the following error on the AWS dashboard:
ERROR Your WSGIPath refers to a file that does not exist. and the application does not work (404 for any path).
How can I deploy the above Flask application to elastic beanstalk?
I encountered a similar problem deploying a Flask application to EB, with a similar directory structure, and had to do 2 things:
Update my manage.py to create an object of name application, not app
import os
from application import create_app, db
from flask.ext.script import Manager, Shell
application = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(application)
Create .ebextensions/myapp.config, and define the following block to point to manage.py
option_settings:
"aws:elasticbeanstalk:container:python":
WSGIPath: manage.py
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "application/static/"
This let Elastic Beanstalk find the application callable correctly.
This is described briefly at the official docs, and is described in more detail in this blog post
EDIT - see project structure below
ProjectRoot
.ebextensions
application.config
application
main
forms.py
views.py
static
templates
tests
manage.py
requirements.txt
config.py
etc, etc
Add the following to .ebextensions/<env-name>.config:
option_settings:
"aws:elasticbeanstalk:container:python":
WSGIPath: myApp/handlers/views.py
Update:
If you don't have .ebextensions directory, please create one for the project. You can find more information of what can be done regarding the container configuration in Customizing and Configuring AWS Elastic Beanstalk Environments guide.
Your WSGIPath refers to a file that does not exist.
This error appears because Beanstalk, by default, looks for application.py. Check at Beanstalk web UI, Configuration > Software Configuration, WSGIPath is mapped to application.py
Update the WSGIPath as shown in the previous replies or rename to application.py file.
As of awsebcli 3.0, you can actually edit your configuration settings to represent your WSGI path via eb config. The config command will then pull (and open it in your default command line text editor, i.e nano) an editable config based on your current configuration settings. You'll then search for WSGI and update it's path that way. After saving the file and exiting, your WSGI path will be updated automatically.
WSGI configuration was painful for me. I did changed WSCI settings using eb config command but it did not work. Below you can fix this in 5 easy steps.
1- Moved app.py function to the root of the directory (where I runned eb init command.
2- Also renamed app.py as application.py and in that initilized application as application = Flask(__name__) not app = Flask(__name__)
3- eb deploy did not worked after this (in the same project) I tried to fix config by using eb config but it was too hairy to sort it out. Delete all .extensions, .gitignore etc from your project.
4- re initialize your project on EB with eb init and follow the prompts. when deployment is done, eb open would launch your webapp (hopefully!)
When I encountered this problem it was because I was using the GUI to upload a zip of my project files. Initially I was zipping the project level directory and uploading that zip to EB.
Then I switched to simply uploading a zip of the project files themselves-ie select all files and send those to a zip-and then the GUI upload utility was able to find my application.py file without a problem because the application.py file was not in a subfolder.
Well, In my case I followed the entire process and conventions but was still getting 404. The problem was my virtual environment. I was ignoring all environment config related folders/files in my .gitignore but not in .ebignore. After creating .ebignore and ignoring all the folders/files which were not related to project code, fixed the issue.

Categories