Prerequisites To Deploy Django App in Amazon Elastic-Beanstalk
Python 3.7 or later
Set up a Python virtual environment
create one folder where you can create a project & virtual environment
now install the virtual environment in that folder
sudo apt install -y python3-venv
python3 -m venv myenv
Activate virtual environment.
Notice that the prefix now included (myenv). This lets you know that the environment is active.
Now install Django
pip install django==4.0.6
To verify that Django is installed, enter the following.
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.
Run your Django site locally with manage.py runserver
python3 manage.py runserver
In a web browser, open http://127.0.0.1:8000/ to view the site
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
In the .ebextensions directory, add a configuration file named django.config with the following text.
You've added everything you need to deploy your application on Elastic Beanstalk. Your project directory should now look like this
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
Existing User or Create New User
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 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
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
If you see your app working, well done, you've deployed your first Django app with Elastic Beanstalk!
Now proceed to AWS console & open Amazon Elastic Beanstock Service you can see app's Environment , Health , URL etc
Links for further details on this deployment
Thanks & Regards
Happy Learning 😊 👍