Deploy a django 1.10 app to AWS beanstalk using python 3.4 and MySQL on a Mac.

Tutorial to accomplish what the title suggests it would accomplish.

It is heavily inspired by this post. But if you want to know why doing it, to what avail, and really, why not hiring somebody that does it for you. I can’t help.

Also note. These things expire soon. LAST EDITED: Apr-2017.

1. Create virtual environment & activate

$ virtualenv -p python3 venv
$ cd venv
$ source bin/activate

venv will be the name of your folder. I have used python 3.4 because currently aws supports that. Don’t use python 2 with this tutorial or things will explode.

2. Copy a django project inside ‘venv’

You can get a django project from wherever, as long as it uses django 1.10 and python 3. I’d suggest you use this one from codingforentrepreneurs.

venv
├── src
│ ├── kirr
│ ├── shortener
│ ├── manage.py
├── requirements.txt

3. Create requirements.txt and pip install

(venv)$ pip install -r requirements.txt

4. Add “.extensions” folder

First, create the folder (src/.ebextensions) and open it:

(venv)$ mkdir .ebextensions
(venv)$ cd .ebextensions
(venv)$ open .

5. The .config files

Then copy the following two files:

project-venv/.ebextensions/01_packages.config

packages:
yum:
git: []
mysql-server: []
mysql: []

and project-venv/.ebextensions/02_python.config

container_commands:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py migrate --noinput"
leader_only: true
02_createsu:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py createsu"
leader_only: true
03_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py collectstatic --noinput"
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "iotd.settings"
"PYTHONPATH": "/opt/python/current/app/iotd:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: iotd/iotd/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"

The first file tells aws to install the required packages for mysql.
The second file contains commands (“container_commands:”) that tell aws to run your django app from beanstalk.

The container_commands: tell aws to migrate, create super user, and collectstatic. This works because a script to create superuser (‘admin’, password: ‘admin’) was added here src/kirr/createsu.py (see here).

The option_settings: set (1) WSGIPath, (2) the static directory, and (3) the python path in 02_python.config to match your project’s directories.

(OPTIONAL) I like to see hidden folders via finders, so in my terminal I enabled it as:
 $ defaults write com.apple.finder AppleShowAllFiles TRUE 
 
$ killall Finder

6. Change settings in kirr/settings.py

Follow this post again.

if 'RDS_DB_NAME' in os.environ:     
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'],
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'admin',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}

8. Add files to git

Aws EBS uses git to know when something has changed. From the root directory (venv):

(venv)$: git add .
(venv)$: git add -f .ebextensions/
$ git commit -m “first commit”

The -f (“force”) is to ensure that hidden folders are added.

9. Install awsebcli for AWS Elastic Beanstalk

Install awsebcli and initialise EB.

$ pip install awsebcli
$ eb init
$ eb create (this will take a long time)
$ eb deploy

Well, good luck. Let me know how it goes, and do check this post for more.