Deploy Django (w/ Django Rest Framework) and Connect to Database using AWS Elastic Beanstalk
I’ve been building a new site for a client for a music studio. The requirement is to have a login system and backend support for retrieving data. The client provides AWS credentials and wants to host the web on AWS. Thinking that Django’s built-in Users model can do exactly that, I’ve decided to use it along with a frontend built in ReactJS. Simple enough.
It’s been around 1.5 years since I’m asked to host something on AWS, so I decided to do some research and found that Elastic Beanstalk should help me to deploy with ease.
So let’s get into it.
I am largely using this documentation as reference. My django app is built from scratch and is simple enough that following the doc doesn’t create some unknown issues.
I already have the AWS credential, so I can skip the create-account part. The django part is not really related either. As long as you can run your app locally, we are in a good shape.
What’s annoying but crucial is the next part.
To use AWS Elastic Beanstalk, we first need to install elastic beanstalk CLI, or EB CLI. You can follow the example here to install EB CLI
This repository hosts scripts to generate self-contained installations of the EB CLI. If you don't have Git, install it…
After installing, run
eb init and set up using your credential. I’m using default for most of the things here.
Next, in your main application folder, create a folder named
.ebextensions by simply running
mkdir .ebextensions and then create a config file inside called
django.config (In fact, elastic beanstalk, when deploying, will pick up all the
.config files and run them. The name is just an example as we are using Django app).
Largely following the guide with minor tweak, my
django.config looks like this:
command: "source /var/app/venv/path/to/venv/bin/activate && python3 manage.py migrate --noinput"
command: "source /var/app/path/to/venv/bin/activate && python3 manage.py collectstatic --noinput"
Amazon Linux2 config is a bit different from the rest in naming convention, so make sure you refer to the guide to get the actual path. Here, I have an option command section that tells eb about my venv setting, WSGI setting, and static file path. I have another section for the container telling what commands to run. Here I simply make sure all the new migration files are run and all static files are in the right place.
Originally there was also another command for creating super user, as we don’t want to get into the box or db to create the superuser, but we do need a way to have at least one super user to run django admin CMS.
We can simply create a script, let’s call it
create_super_user.py and the script looks like
from django.contrib.auth.models import User
from django.conf import settingsdef run():
super_user = User.objects.filter(username='some_admin_username').first()
super_user.is_staff = True
super_user.is_superuser = True
User.objects.create_superuser("some_admin_username", "super_admin_email", "some_admin_pasword")
and in the
container_commands, I added another line:
03_create_super_user: command: "source /var/app/venv/staging-LQM1lest/bin/activate && python3 manage.py runscript create_super_user" leader_only: true
Note that I have
django-extension installed already so I’m utilizing the
Now we have the config in place, let’s run
eb create <whatever_you_wanna_name_it> and let elastic beanstalk create the ec2 instane for you.
When all done, run
eb status and grab the
CNAME in it. This is the host that you want to put into your
After that, you should run
eb deploy and let it build. Once done, you should be able to
eb open to visit your django app.
NOTE: I ran into a problem where my code is “not updating”. Turns out I’m using git to manage my code and eb actually uses the code in git if it detects a .git in your folder. Therefore, if you are using git to manage your code, make sure you commit and push your changes and THEN run
eb deploy .
I also have a postgres db set up in AWS, following this tutorial:
Adding an Amazon RDS DB instance to your Python application environment
You can use an Amazon Relational Database Service (Amazon RDS) DB instance to store data gathered and modified by your…
I’m not gonna get into it. Just some quick notes:
- If you ssh into your instance, you will not see the RDS environment variables. You will need to have some CLI that connects to the database separately.
- I find it the easiest to use AWS’s key management to store the values, so you can easily retrieve it while keeping it safe