Django with Elastic Beanstalk

Over the last week or so, I have explored the process of getting a Django project deployed to Amazon Elastic Beanstalk. The initial deployment of the project is fairly simple:

$ eb init
$ eb create --database -k {key-name}

Those two commands, coupled with some s3 static/media settings can get your Django project up and running fairly quickly. In this article I will be running though some of the issues I have faced so far and how those issues have been resolved.

Accessing the instance and executing Django commands

Fairly early on in the development of the project I needed to access the instance to run a manual django migration. Usually I would just SSH into our server, source the env and execute the command. On Amazon we’re doing the same thing, but there are a few more steps to go through.

The first step is simple, SSH into the instance via:

$ eb ssh

Next, you’ll need to navigate to the current deployment of the project:

$ cd /opt/python/current/app

Then, sourcing the correct environment is a 2 command process:

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

From there you are ready to run your manage.py commands:

$ src/manage.py migrate

Connecting to the RDS instance

Deploying the project in the way I outlined above will create an RDS instance for the project. There a couple of steps needed to access this database as it only allows the EB instance to connect to it by default.

Firstly, you’ll need to find out which RDS instance you are going to be accessing. To do this, navigate to your project environment in the EB section of the AWS console. Once there, navigate to the configuration tab, and locate the RDS section:

In this section, the bit we want to note is the first part of the endpoint (aavf8pq1n1fwwe).

Navigate to the RDS section of the console and find the instance with the name corresponding to the characters we noted above. Click the instance, go into the second tab (details) and click on the second security group in the list.

In this window you should be able to edit the inbound rules for the RDS instance. If you edit this, you can add a MYSQL/Aurora connection with your IP as the source, which will allow you to connect to the database.

Once you have added that inbound rule, the following command will grant you access to the DB (using the user credentials you used to set up the initial database):

$ mysql -h [full-instance-endpoint] -P 3306 -u [instance-user] -p

Configuring the instance to allow Django filer to upload properly One of the first problems I noticed with Django Filer after deploying the project was that jpeg uploads did not work. After doing some research I found that the problem was that the default installation of the EB instance does not have the software required to decode jpeg images. To remedy this, in our 01-django_eb.config file I added the following:

packages:
    yum:
        libjpeg-turbo-devel: []
        libpng-devel: []
        freetype-devel: []

These packages will be installed as the instance is deployed and will allow the instance to manipulate jpeg images, png images and fonts respectively.