Step by step guide to deploy a Django application to Elastic Beanstalk - AWS Elastic Beanstalk
Tags: aws, elasticbeanstalk, django, python
Published: 2023-12-02 19:19:17

## 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:
{% embed https://youtu.be/eH0hTPufZcY?si=XIpFTSW-6BkyYCTK %}

## 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
```

2. 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.

3. Use `pip`to install Django:
```
(eb-virt)~$ pip install django==2.2
```

4. To verify that Django is installed, enter the following:
```
(eb-virt)~$ pip freeze
Django==2.2
...
```

5. Use the `django-admin startproject` command to create a Django project named `ebdjango`:
```
(eb-virt)~$ django-admin startproject ebdjango
```

6. 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']
```
 
7. Run your Django site locally with `manage.py runserver`:
```
(eb-virt) ~$ cd ebdjango
(eb-virt) ~/ebdjango$ python manage.py runserver
```

8. In a web browser, open [http://127.0.0.1:8000/](http://127.0.0.1:8000/) to view the site.

9. 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.

1. Run `pip freeze`, and then save the output to a file named `requirements.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.

2. Create a directory named `.ebextensions`:
```
(eb-virt) ~/ebdjango$ mkdir .ebextensions
```

3. 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
```

4. 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
```

5. 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`.

6. 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.
](https://stackoverflow.com/a/76620598/12326605)


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.

7. 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.**

8. 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']
```

9. 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
```

10. 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.