DEV Community

Cover image for Steps To Deploy Django App in Amazon Elastic Beanstalk
Sanjay Prakash
Sanjay Prakash

Posted on

Steps To Deploy Django App in Amazon Elastic Beanstalk

Prerequisites To Deploy Django App in Amazon Elastic-Beanstalk

Python 3.7 or later

pip

virtualenv

awsebcli

AWS CLI

Set up a Python virtual environment

create one folder where you can create a project & virtual environment

cd Documents

mkdir AWS-elasticBeanstalk

now install the virtual environment in that folder

sudo apt install -y python3-venv
python3 -m venv myenv

Activate virtual environment.

source myenv/bin/activate

Notice that the prefix now included (myenv). This lets you know that the environment is active.

Capture

Now install Django

pip install django==4.0.6

To verify that Django is installed, enter the following.
pip freeze
Django==4.0.6

now switch over to VS-Code & activate your virtual env

Create a Django project in your virtual environment

django-admin startproject ebdjango

This command creates a standard Django site named ebdjango with the following directory structure.

cap-2

Run your Django site locally with manage.py runserver

cd ebdjango

python3 manage.py runserver

In a web browser, open http://127.0.0.1:8000/ to view the site

Capture-8000

Configure your Django application for Elastic Beanstalk

Activate your virtual environment in VS Code project workspace terminal
enter command to see all your required dependency installed or not

pip freeze > requirements.txt

Next Create a directory named .ebextensions

mkdir .ebextensions

In the .ebextensions directory, add a configuration file named django.config with the following text.

Screenshot from 2022-08-06 23-31-48

You've added everything you need to deploy your application on Elastic Beanstalk. Your project directory should now look like this

Screenshot from 2022-08-06 23-34-46

We didn't create git repo yet so do not worry if .gitignore not in your project folder

Proceed to the IAM service on the AWS console and perform the steps below to create access keys

IAM
Users
Existing User or Create New User
Security Credentials
Create Access Keys
copy both your access keys & save it in your local PC

After creating access keys enter below command in your terminal & follow the steps

aws configure

AWS Access Key ID [None]: ENTER_YOUR_ACCESS_KEY_ID

AWS Secret Access Key [None]:ENTER_YOUR_SECRET_ACCESS_KEY

Default region name [None]: ENTER_YOUR_DEFAULT_REGION

Default output format [None]: json

Create an environment and deploy your Django application
Initialize your EB CLI repository with the eb init command.

eb init -p python-3.8 django-app

eb create django-env

eb status

Screenshot from 2022-08-06 23-52-02

Open the settings.py file in the ebdjango directory. Locate the ALLOWED_HOSTS setting, and then
add your application's cname value save the file, and then deploy your application by running below commands

eb deploy

eb open

If you see your app working, well done, you've deployed your first Django app with Elastic Beanstalk!

Screenshot from 2022-08-06 23-54-17

Now proceed to AWS console & open Amazon Elastic Beanstock Service you can see app's Environment , Health , URL etc

new

Capture-eb

Links for further details on this deployment

AWS Link

Youtube Link

Thanks & Regards

Happy Learning 😊 👍

Top comments (0)