Deploying Django To Elastic Beanstalk - StarWind Blog
Deploying Django To Elastic Beanstalk - StarWind Blog
Home > StarWind Blog > Deploying Django Project to AWS Elastic Beanstalk
In this blog post, we will talk about getting your Django project deployed to Amazon Web Services (AWS)
platform. There are di erent ways you can do that with AWS: one is deployment to EC2 instance running
Linux (you just connect to it over SSH and work with Linux server directly), and the other one is deployment
to Elastic Beanstalk. The rst scenario is close to simple project setup on a local Linux machine with some
extra steps involved – for that you can refer to my previous posts which cover most of the steps involved. In
this post, we will focus on Elastic Beanstalk deployment scenario, going through all the steps involved in this
process when it is performed from Ubuntu.
Before we dive into the deployment process, it is worth saying a few words about AWS Elastic Beanstalk.
AWS Elastic Beanstalk is an abstraction layer which allows you to deploy applications to AWS without
manually creating individual infrastructure components, such as EC2 severs, ELBs, keypairs or security
groups (Beanstalk will create those for you) and later manage a single application entity instead of taking
care of grouping and managing a bunch of disparate resources. Though you can create Elastic Beanstalk
apps both from AWS web console and command line interface, Elastic Beanstalk really shines when you
leverage the command line interface, which makes your deployment process really fast and o ers
possibilities for scripting and automation. At the same time, use of Elastic Beanstalk does not deprive you
from ne-grained control as all underlying infrastructure components are still visible and can be managed
through the AWS console. Beanstalk acts as a container for environment(s) (think of your conventional DEV,
TEST, PROD trinity 😊) and those environments de ne speci c infrastructure components used by the
application.
Now that we are done with the preliminaries, let’s go through the four-step process of deploying a Django
project to AWS Elastic Beanstalk (we will cover application of updates to your project as step 5). I will be going
through all these steps using an Ubuntu 18.04.2 LTS machine.
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 1/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
With these components in place, lets create a virtual environment named eb-virt specifying the use of Python
3.6 by means of the virtualenv command:
In the command above, we specify “eb-virt” as the name of our virtual environment, which will result in the
creation of “eb-virt” directory in your user’s home directory.
Once we are in the context of an activated environment, which is indicated by the changed command line
prompt pre x that now includes a virtual environment name – (eb-virt), we need to install Django. The latest
version of Django is supported by Elastic Beanstalk is 2.1.1, so we will install this version:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 2/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Once the installation is completed, we can verify that Django is installed using the pip freeze command:
Running pip freeze command to con rm that Django was installed correctly
When the pip freeze command is executed, it lists all the packages installed within a virtual environment, and
we will use it later to con gure our Django project for use with Elastic Beanstalk. Now let’s move on to the
next step.
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 3/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Once executed, this command creates a Django project folder with a django app inside of it:
For convenience, I would recommend you rename the top level project folder from “ebdjango” to “ebdjango-
project” as shown below:
This will help you to distinguish between the top-level project level folder and the Django app sub-folder
inside of it.
Let´s now test our Django application locally to make sure that it works. For that, we need to make sure that
our virtual environment is activated and then switch to Django project directory using the cd ebdjango-proje
ct command, apply initial migrations with the python manage.py migrate command, and, next, run Django
server using the python manage.py runserver command:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 4/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Once the server is started, we can access the Django app through a web browser, using its default address
http://127.0.0.1:8000 to con rm that it works. If all worked well, you should see the default Django app page
with the rocket icon as shown below:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 5/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
After accessing the Django site, just have a look at the console output making sure no errors are logged and
if everything is OK there, stop server using the Ctrl + C hot keyboard shortcut in the console window and
move on to the next step.
We rst need to use the pip freeze command to list packages which we have installed in our virtual
environment and pipe output of this command into requirements.txt le as shown below:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 6/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Once the requirements.txt le was created, edit it with nano editor and remove everything except for
Django==2.1.1 – if your requirements.txt le will have unnecessary/not supported packages your app won’t
be running properly on Elastic Beanstalk showing you “Index of /” text instead of default Django page. Here is
how your requirements.txt should look like:
Content of requirements.txt le
Next, we need to create a hidden directory .ebextensions within our project directory using the mkdir .ebext
ensions command and create a django.con g le in this directory:
Contents of django.con g le
Inside of django.con g WSGIPath value corresponds to your Django app folder inside of your Django project
folder – this is where the wsgi.py script is located. Elastic Beanstalk uses this WSGI script to start the
deployed app.
We now nished the con guration of Django project for Beanstalk and can deactivate our virtual
environment and move on to the deployment of our app onto AWS Elastic Beanstalk.
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 7/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Installing awsebcli
With EB CLI installed, we can initialize our EB CLI repository (create Elastic Beanstalk app) using the eb init -p
python-3.6 django-test-app command as shown below:
The above-mentioned command will prompt you for aws-access-id and aws-secret-key – to generate this ID
and secret key, you will need to log on to AWS console (https://console.aws.amazon.com) and once there,
click on your name in the top right corner of the page and select “My Security Credentials” as shown below:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 8/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
This will take you to “Your Security Credentials” page of the Identity and Access Management (IAM)
dashboard, where you can generate aws-access-id and aws-secret-key clicking on the “Create New Access
Key” button:
AWS Console – “Your Security Credentials” page of Identity and Access Management (IAM)
Note that you cannot view or retrieve Access Keys for already created Access Key IDs – you can only view and
save Access Key at Access ID creation stage – for that you need to click on the “Show Access Key” link in
Create Access Key Dialog and save Access Key in a safe place:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 9/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Once we initialized our EB CLI repository using the eb init -p python-3.6 django-test-app command, we can
create an environment and deploy our application using the eb create command as shown below:
We can check on the environment creation status using the eb status command as shown below:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 10/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
It usually takes about 5 minutes to spin up a new environment. We don’t expect to get Green health status at
this stage, instead, we just need to capture CNAME value from eb status command output:
Once we have CNAME value, we need to put it into our application settings.py le. For that, open the le for
edit using the nano settings.py command (be sure to change directory to app directory inside of project
directory) as depicted below:
In the settings.py le, locate ALLOWED_HOSTS = [] line and paste your CNAME value between square
brackets and single quotes so that it looks approximately as follows:
Pres Ctrl + X to save changes and exit nano editor. With this change in place, we are now ready to run the eb
deploy command once again. If all previous steps were performed correctly, the eb status command should
return you Green health status and you should be able to open your Django app using the eb open
command:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 12/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
As you can see, we now have our Django app deployed and running on AWS Elastic Beanstalk.
You may also run into a situation when you are getting Red health status and see “Index of /” text in the
browser as shown below:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 13/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
In this case, make sure your requirements.txt le does not contain unnecessary entries (check step 3).
Essentially, that concludes the Django app deployment process and the only thing we still need to look at is
how we can apply changes to our Django app.
As Django admin app uses some images, we need to con gure a static les storage via STATIC_ROOT setting
in settings.py le. To do that, we need to edit settings.py le and a line STATIC_ROOT = ‘static’ to the very end
of it:
Once STATIC_ROOT has been de ned, we need to run the python manage.py collectstatic command from
the activated virtual environment, making sure we run that on the project folder level as shown below:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 14/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Make sure that the collectstatic command produces correct output with no errors. With that in place, let’s
add admin user using the python manage.py createsuperuser command, if necessary, activating the
virtual environment beforehand, and applying changes to our AWS Elastic Beanstalk app with the eb deploy
command.
Once that has been done, we just need to run eb deploy again and, if all goes well, you should be able to log
into Django admin UI (see sample screenshot below).
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 15/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Django admin UI
VSAN from StarWind eliminates any need for physical shared storage just by mirroring internal ash
and storage resources between hypervisor servers. Furthermore, the solution can be run on the o -
the-shelf hardware. Such design allows VSAN from StarWind to not only achieve high performance and
e cient hardware utilization but also reduce operational and capital expenses.
If your Django administration app shows you only the text without graphics and images, make sure that
STATIC_ROOT is de ned and run the python manage.py collectstatic command.
From this moment on, you can just go through the iterative cycle of doing local changes and applying them
using the eb deploy command.
There are few extra things which I want to mention before we wrap up here.
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 16/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Firstly, we need to take a glance at how things look from the AWS console’s side. On the screenshot below,
we can see our Django application (created with eb init command) and, within it, we have Django
environments (you create those with eb create command) – just for illustration purposes I’ve added a
“production” environment there:
You can drill down into each environment to manage associated settings and underlying components or
view logs.
Secondly, for development purposes, it would be preferable to minimize costs and, for that, you can easily
remove the created Django environments using the eb terminate command. Whenever you need to add
them back, you can use the eb create command for that. Running the eb terminate command just removes
speci ed environment (terminates the environment and all AWS resources that run within it) but leaves the
Elastic Beanstalk application container intact.
And now we are ready to wrap up for today. As you can see, the AWS Elastic Beanstalk deployment option
for Django is quite straightforward and convenient for use. It o ers you simpli ed management which frees
up time otherwise spent on server con guration which is also coupled with good customization capabilities
and quite cost-e cient.
In my next blog post, I will try to cover some additional settings/options of AWS Beanstalk, namely, switching
the database engine from mysql to Postgres, con guring S3 storage for app static and media les and
addition of custom URL for your app.
If you need more information or details I would also recommend you to review some o cial Amazon
documentation on the deployment topic: AWS Documentation – Deploying a Django Application to Elastic
Beanstalk or look through di erent other sections of AWS Elastic Beanstalk Documentation.
Related materials:
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 17/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
How to: Install Django on Ubuntu Server 18.04 LTS – Part 1 Development
environment installation
How to Install Django on Ubuntu Server 18.04 LTS – Part 2: Con guring Django to use
MySQL database
Back to blog
SHARE:
Mikhail Rodionov
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 18/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Author:
Mikhail Rodionov
Search
StarWind VSAN
Download Trial
Request a Demo
More Info
Subscribe to posts
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 19/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
SUBMIT
35 491
Subscribers
Categories
All (39)
Hardware (67)
Services (137)
Software (431)
Trending Articles
How to Con gure Storage Replication using Windows Server 2016? – Part 2
based on 2 review
Latest сomments
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 20/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
Andy Higgins
Hi Romain, Interesting read - I just completed the same setup with a company I am working with. I...
Getting started with Azure AD Connect
1 day ago
monotosh mitra
@rashidrodriguez:disqus
https://tinkertry.com/easie...
vSphere 6.7: hot migrate VMs to different clusters with vMotion
2 weeks ago
Mikhail
Thanks for feedback. I have plans for 3rd part to touch on configuring static filea along with...
Deploying Django Project to AWS Elastic Beanstalk, Part 2: Database Settings Configuration
2 weeks ago
Follow us
Need assistance?
WE ARE SOCIAL
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 21/22
3/15/2020 Deploying Django to Elastic Beanstalk | StarWind Blog
StarWind Storage Appliance StarWind Command Center New StarWind Deduplication AoE Initiator
(SA) StarWind Virtual Tape Library Analyzer Free FCoE Initiator
StarWind Virtual Tape Library (VTL) StarWind Tape Redirector Free
Appliance (VTLA) Cloud VTL for AWS and Veeam StarWind RAM Disk Free
Storage Gateway for Backblaze StarWind rPerf Free PARTNERS
SITE MAP
Terms of Use
Privacy Policy
ProActive Terms of Service
SLA
Refund Policy
https://www.starwindsoftware.com/blog/deploying-django-project-to-aws-elastic-beanstalk 22/22