Deploying a Django application to Elastic Beanstalk
This article walks through the deployment of a simple Django application to an AWS Elastic Beanstalk environment running Python.
In this article , you’ll do the following:
- Set up a Python virtual environment and install Django
- Create a Django project
- Configure your Django application for Elastic Beanstalk
- Deploy your site with the EB CLI
- Update your application
- Clean up
You can also watch the below video and follow along:
Prerequisites
To follow this tutorial, you should have all of the Common Prerequisites for Python installed, including the following packages:
- Python 3.7 or later
- pip
- virtualenv
- awsebcli
I have already installed all these packages in my local dev environment (Ubuntu).
Create and Run a sample Django application locally
- Create a virtual environment named eb-virt:
virtualenv ~/eb-virt
- Activate the virtual environment:
source ~/eb-virt/bin/activate
You'll see (eb-virt) prepended to your command prompt, indicating that you're in a virtual environment.
- Use
pip
to install Django:
(eb-virt)~$ pip install django==2.2
- To verify that Django is installed, enter the following:
(eb-virt)~$ pip freeze
Django==2.2
...
- Use the
django-admin startproject
command to create a Django project namedebdjango
:
(eb-virt)~$ django-admin startproject ebdjango
- Open the
settings.py
file in theebdjango
directory. Locate theALLOWED_HOSTS
setting, and then add your localhost(127.0.0.1
) domain name:
...
ALLOWED_HOSTS = ['127.0.0.1']
- Run your Django site locally with
manage.py runserver
:
(eb-virt) ~$ cd ebdjango
(eb-virt) ~/ebdjango$ python manage.py runserver
In a web browser, open http://127.0.0.1:8000/ to view the site.
Check the server log to see the output from your request. To stop the web server and return to your virtual environment, press
Ctrl+C
.
Django version 2.2, using settings 'ebdjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[07/Sep/2018 20:14:09] "GET / HTTP/1.1" 200 16348
Ctrl+C
Configure and Deploy your Django application to Elastic Beanstalk with the EB CLI
Now that you have a Django-powered site on your local machine, you can configure it for deployment with Elastic Beanstalk.
- Run
pip freeze
, and then save the output to a file namedrequirements.txt
:
(eb-virt) ~/ebdjango$ pip freeze > requirements.txt
Elastic Beanstalk uses requirements.txt
to determine which package to install on the EC2 instances that run your application.
- Create a directory named
.ebextensions
:
(eb-virt) ~/ebdjango$ mkdir .ebextensions
- In the
.ebextensions
directory, add a configuration file nameddjango.config
with the following text:
Example: ~/ebdjango/.ebextensions/django.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: ebdjango.wsgi:application
- Now you've added everything you need to deploy your application on Elastic Beanstalk. Your project directory should now look like this:
~/ebdjango/
|-- .ebextensions
| `-- django.config
|-- ebdjango
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
|-- db.sqlite3
|-- manage.py
`-- requirements.txt
- Initialize your EB CLI repository with the
eb init
command:
~/ebdjango$ eb init -p python-3.7 django-tutorial
Application django-tutorial has been created.
This command will create an application named django-tutorial
.
- Create an environment and deploy your application to it with
eb create
:
~/ebdjango$ eb create django-env
If you see a "service role required" error message, run eb create interactively (without specifying an environment name) and the EB CLI creates the role for you.
StakeOverflow - The instance profile aws-elasticbeanstalk-ec2-role associated with the environment does not exist.
The above command creates a load-balanced Elastic Beanstalk environment named django-env
. Creating an environment takes about 5 minutes. As Elastic Beanstalk creates the resources needed to run your application, it outputs informational messages that the EB CLI relays to your terminal.
- When the environment creation process completes, find the domain name of your new environment by running eb status:
~/ebdjango$ eb status
Environment details for: django-env
Application name: django-tutorial
...
CNAME: eb-django-app-dev.elasticbeanstalk.com
...
Here note, Your environment's domain name is the value of the CNAME property.
- Open the
settings.py
file in theebdjango
directory. Locate theALLOWED_HOSTS
setting, and then add your application's domain name that you found in the previous step to the setting's value. If you can't find this setting in the file, add it to a new line:
...
ALLOWED_HOSTS = ['eb-django-app-dev.elasticbeanstalk.com']
- Save the file, and then deploy your application by running eb deploy. When you run
eb deploy
, the EB CLI bundles up the contents of your project directory and deploys it to your environment.
~/ebdjango$ eb deploy
- When the environment update process completes, open your website with eb open:
~/ebdjango$ eb open
This opens a browser window using the domain name created for your application. You should see the same Django website that you created and tested locally.
Top comments (0)