Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Django Superuser on AWS Elastic Beanstalk

I have a custom user class called MyUser. It works fine locally with registrations, logins and so on. I'm trying to deploy my application to AWS Elastic Beanstalk and I'm running into some problems with creating my superuser.

I tried making a script file and run it as the official AWS guide suggests. Didnt work well so I decided to try a secondary method suggested here and create a custom manage.py command to create my user.

When I deploy I get the following errors in the log.

[Instance: i-8a0a6d6e Module: AWSEBAutoScalingGroup ConfigSet: null] Command failed on instance. Return code: 1 Output: [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] command failed with error code 1: Error occurred during build: Command 02_createsu failed.

[2015-03-10T08:05:20.464Z] INFO  [17937] : Command processor returning results: 
{"status":"FAILURE","api_version":"1.0","truncated":"false","results":[{"status":"FAILURE","msg":"[CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] command failed with error code 1: Error occurred during build: Command 02_createsu failed","returncode":1,"events":[]}]}


[2015-03-10T08:05:20.463Z] ERROR [17937] : Command execution failed: [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] command failed with error code 1: Error occurred during build: Command 02_createsu failed (ElasticBeanstalk::ActivityFatalError)
at /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:189:in `rescue in exec'
...
caused by: command failed with error code 1: Error occurred during build: Command 02_createsu failed (Executor::NonZeroExitStatus)

The code looks like the following:

This is my mysite.config file in .ebextensions/

01_syncdb and 03_collectstatic works fine.

container_commands:
  01_syncdb:    
    command: "django-admin.py migrate --noinput"
    leader_only: true
  02_createsu:
    command: "manage.py createsu"
    leader_only: true
  03_collectstatic:
    command: "django-admin.py collectstatic --noinput"
option_settings:
  - namespace: aws:elasticbeanstalk:container:python
    option_name: WSGIPath
    value: treerating/wsgi.py
  - option_name: DJANGO_SETTINGS_MODULE
    value: treerating.settings

This is my /profiles/management/commands/createsu.py file:

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from profiles.models import MyUser

class Command(BaseCommand):
    def handle(self, *args, **options):
        if MyUser.objects.count() == 0:
            MyUser.objects.create_superuser("admin", "treerating", "password")

And I have __init__.py files in both /management/ and /commands/ folders.

I tried this command locally from command line and it works fine and creates the user without errors. So there shouldnt be any issue with the command itself or the MyUser.objects.create_superuser().

EDIT: I tried changing my def handle(): function to only set a variable to True and I still get the same errors. So it seems like the problem is not related to the create_superuser function or the handle, but more something with using manage.py.

Any ideas?

EDIT 2: I tried executing the command by SSH and failed. I then followed the instructions in this post and set the Python Path's manually with:

source /opt/python/run/venv/bin/activate and source /opt/python/current/env

I was then able to successfully create my user. The official AWS Django Deployment guide does not mention anything about this. But I guess you are suppose to set your Python Path's in the .config file somehow. I'm not sure exactly how to do this so if someone still want to answer that, I will test it and accept it as answer if that will solve the deployment errors.

like image 782
Marcus Lind Avatar asked Mar 10 '15 08:03

Marcus Lind


People also ask

Can I host Django on AWS?

Django comes with a development server that helps run Django projects in localhost. However, to make your web application available worldwide you will need a host machine. Amazon Web Services (AWS) provides EC2 (Elastic Compute Cloud) which is the most popular choice to host Django web applications.

How does Django integrate with AWS?

With the key-pair file, the connection to the instance was made using SSH. Dependencies such as Apache, pip, mod-wsgi, and virtualenv are installed on the EC2 instance. After creating a virtual environment, the repository is cloned, and django app dependencies are installed. There on, Django development server is run.

How do I add a code to my AWS Elastic Beanstalk?

Open the Elastic Beanstalk console , and in the Regions list, select your AWS Region. In the navigation pane, choose Environments, and then choose the name of your environment from the list. If you have many environments, use the search bar to filter the environment list. Choose Upload and deploy.


2 Answers

Double-check the link to your secondary method. You can set the python path in the option settings (.ebextensions/02_python.config) that you've created:

option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "iotd.settings"
    "PYTHONPATH": "/opt/python/current/app/iotd:$PYTHONPATH"
    "ALLOWED_HOSTS": ".elasticbeanstalk.com"

However, I've done this and am still experiencing the issue you've described, so you'll have to see if it fixes it.

EDIT: It turns out my issue was a file structure issue. I had the management directory in the project directory, when it should have been placed one level deeper in the directory of one of my apps.

This placed it one level deeper beneath my manage.py and settings.py than is shown in the example, but it is working fine now.

like image 169
Adam Starrh Avatar answered Nov 15 '22 09:11

Adam Starrh


I know this could be late but I just wanted to share that I solved this issue by adding the file /profiles/management/commands/createsu.py into the app folder you are using.

In my case was:

easy/easyapp/management/commands/createsu.py

where easy is my project and easyapp my app.

like image 34
Miguel Alcantar Avatar answered Nov 15 '22 08:11

Miguel Alcantar