Boilerplate django project for AWS Elastic Beanstalk.
Follow the steps to use this repot
-
Clone this repo
git clone https://github.com/Alexmhack/Django-ElasticBeanstalk-Boilerplate.git
cd Django-ElasticBeanstalk-Boilerplate
-
Create and activate virtualenv by running these commands, you can even use pipenv but I prefer virtualenv
pip install virtualenv
python3 -m virtualenv env
cd env/Scripts
activate
-
Now install the python packages in virutalenv from requirements.txt file
pip install -r requirements.txt
-
Install AWS EB CLI
-
Create Elastic Beanstalk application
eb init
Choose your region Choose python version 3.6 Setup ssh for your instance, choose a keypair or create one -
Create EB Environment by running
eb create TESTenv --database.engine postgres
# <- TESTenv is the name of environment postgres is the database engine for your database, enter the username and password of your choice when promptedThis process will take some time
-
Change config for your elastic beanstalk environment
eb config
Change the WSGIPath from
application.py
to our projects path,starter/wsgi.py
Save the file and close it, the environment update will start in cmd -
Add env url to
ALLOWED_HOSTS
eb status
Copy theCNAME: <YOUR CNAME>
and paste it inALLOWED_HOSTS
list in starter/settings.py file. -
Deploy
eb deploy
-
Open the instance url in web browser
eb open
-
You should see the standard Django2 start project welcome page.
-
Now get the RDS database host endpoint and port from AWS EB console > Configuration > Database (at the bottom of webpage). Copy the endpoint and paste it in the
DATABASES
setting in starter/settings.py file.
Follow these steps to create a Elastic Beanstalk environment inside a VPC and then run a Django backend instance.
EB CLI Create Reference has all the options for our needs.
I assume you have downloaded awsebcli using pip, if not run pip install --upgrade awsebcli
Create a VPC in the AWS, here is official guide. Follow this guide before running the below commands
-
Run the eb command
eb create deploy -db.engine postgres -db.user xxxxxxxxx -db.pass xxxxxxxxx --vpc --vpc.dbsubnets xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxxxxxxxx -k KEY_PAIR_NAME
-db.engine
-> the database engine, here postgres-db.user
-> username of the database instance-db.pass
-> password of the db instance--vpc
-> launches environment in the Virtual Private Cloud (asks for details of vpc)--vpc.dbsubnets xxxxxxxxx,xxxxxxxxx,xxxxxxxxx,xxxxxxxxx
-> the subnets of the db subnet groups-k KEY_PAIR_NAME
-> If you want to connect to your instances using ssh use this option with your KEY_PAIR_NAME -
Next follow the same commands stated in the above section to config and deploy your environment.