DEV Community

Arpit-Gulgulia
Arpit-Gulgulia

Posted on

Step by step guide to deploy a Django application to Elastic Beanstalk - AWS Elastic Beanstalk

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:

Step by step guide to deploy a Django application to Elastic Beanstalk - AWS Elastic Beanstalk - YouTube

This video walks you through the deployment of a Django website to an AWS Elastic Beanstalk environment running Python. This tutorial shows you how to host a...

favicon youtube.com

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

  1. Create a virtual environment named eb-virt:
virtualenv ~/eb-virt
Enter fullscreen mode Exit fullscreen mode
  1. Activate the virtual environment:
source ~/eb-virt/bin/activate
Enter fullscreen mode Exit fullscreen mode

You'll see (eb-virt) prepended to your command prompt, indicating that you're in a virtual environment.

  1. Use pipto install Django:
(eb-virt)~$ pip install django==2.2
Enter fullscreen mode Exit fullscreen mode
  1. To verify that Django is installed, enter the following:
(eb-virt)~$ pip freeze
Django==2.2
...
Enter fullscreen mode Exit fullscreen mode
  1. Use the django-admin startproject command to create a Django project named ebdjango:
(eb-virt)~$ django-admin startproject ebdjango
Enter fullscreen mode Exit fullscreen mode
  1. Open the settings.py file in the ebdjango directory. Locate the ALLOWED_HOSTS setting, and then add your localhost(127.0.0.1) domain name:
...
ALLOWED_HOSTS = ['127.0.0.1']
Enter fullscreen mode Exit fullscreen mode
  1. Run your Django site locally with manage.py runserver:
(eb-virt) ~$ cd ebdjango
(eb-virt) ~/ebdjango$ python manage.py runserver
Enter fullscreen mode Exit fullscreen mode
  1. In a web browser, open http://127.0.0.1:8000/ to view the site.

  2. 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
Enter fullscreen mode Exit fullscreen mode

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.

  1. Run pip freeze, and then save the output to a file named requirements.txt:
(eb-virt) ~/ebdjango$ pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Elastic Beanstalk uses requirements.txt to determine which package to install on the EC2 instances that run your application.

  1. Create a directory named .ebextensions:
(eb-virt) ~/ebdjango$ mkdir .ebextensions
Enter fullscreen mode Exit fullscreen mode
  1. In the .ebextensions directory, add a configuration file named django.config with the following text:

Example: ~/ebdjango/.ebextensions/django.config

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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.
Enter fullscreen mode Exit fullscreen mode

This command will create an application named django-tutorial.

  1. Create an environment and deploy your application to it with eb create:
~/ebdjango$ eb create django-env
Enter fullscreen mode Exit fullscreen mode

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.

  1. 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
  ...
Enter fullscreen mode Exit fullscreen mode

Here note, Your environment's domain name is the value of the CNAME property.

  1. Open the settings.py file in the ebdjango directory. Locate the ALLOWED_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']
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. When the environment update process completes, open your website with eb open:
~/ebdjango$ eb open
Enter fullscreen mode Exit fullscreen mode

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)