Deploy Django application to aws cloud in 25 steps in much simpler way
Introduction:
AWS Elastic Beanstalk is an easy-to-use service for deploying and scaling web applications and services developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.
Here ,We can try doing the deployment of the Django web application to Elastic bean stalk.
Rather beating around the bush lets jump on to series of steps to deploy Django Application.
The below steps will get the source of a Django Application from git and run in your local machine and then make necessary steps to deploy an application to Elastic bean Stalk and I am going to refer Elastic bean stalk as EBS for the rest of the article.
EBS is PaaS(Platform as a service) and I have tried my best to make it simple to avoid people getting confused with official doc.
Prerequisites:
- Python 3.6 (https://www.python.org/downloads/)
- pip (Comes along with python installation)
- virtualenv (pip install virtualenv)
- awsebcli ( pip install awsebcli)
- IAM user with Administrator Role.
- Platform -windows
Steps:
1.lets create a directory for our project.
> mkdir pylabz
2. Moving to Pylabz directory
> cd pylabz
3.cloning the django application
> git clone https://github.com/deepakr6242/apollo.git
4.Move the django project
> cd apollo
Now You must be able to see like this.
Now we will try to run the sourced application in your machine to ensure if the application is up and running.
5.Copy the path of above cloned application and paste in your cmd like
cd >path of the apollo application>
eg >cd C:\Users\sysadmin\pylabz\apollo
6.Run the django application using the command below
>python manage.py runserver
After running the above command you should be able to see like below
7.The application is served in local machine and you can access it through http://localhost:8000
Our application is up and running and Now lets deploy the locally running Django Application to EBS
8.Now we will set up the virtual environment named dexter-virt assuming that you are in windows
Use the below command
Assuming you are in path C://
>mkdir dexter
>cd dexter
> virtualenv %HOMEPATH%\dexter-virt
and you should see the output as below and now the Virtual environment is ready
9.Now we will activate the virtual Environment.
> %HOMEPATH%\dexter-virt\Scripts\activate
(dexter-virt) c:\dexter>
10. Now we will django inside the virtual environment using
> pip install django==2.1.1
doing so will install django and pytz
11.To verify after installation do the below
> (eb-virt)~$
pip freeze
will show django and pytz
12.Now we will paste the project Apollo which we have cloned from the git hub already in step 4 and doing so will look like below
13. Run pip freeze
and save the output to a file named requirements.txt
Optional:Remove .git folder in this step
(dexter-virt) c:\dexter>pip freeze > requirements.txt
14.Create a new directory, called .ebextensions
:
(dexter-virt) c:\dexter> mkdir .ebextensions
15 Inside .ebextensions create a file called django.config and paste the below content
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: apollo/wsgi.py
and ensure the indentation exactly as below
**WSGI is WebServer Gateway interface — the file forwarding the request to application and EBS uses this file to start the application.
16.Now deactivate the environment
(dexter-virt) c:\dexter>deactivate
c:\dexter>
17. We have configured our project which encloses the application in a way that it can be deployed to EBS.Apparently the project is in state of deployable at this stage.
Note:Assuming you have done Pip install awsebcli as part of prerequisites.
18.Your Project directory should look like this now.
Directory of c:\dexter
09/15/2019 03:30 AM <DIR> .ebextensions
09/15/2019 03:20 AM <DIR> apollo
09/15/2019 03:20 AM <DIR> apollo4
09/14/2019 06:39 PM 40,960 db.sqlite3
09/14/2019 06:39 PM 826 manage.py
09/14/2019 06:39 PM 40 README.md
09/15/2019 03:24 AM 29 requirements.txt
19.Now we need to create an EBS environment in our local machine to deploy our Application in EBS and the below will cre
c:\dexter>eb init -p python-3.6 dexter-application
Application dexter-application has been created.
20.Initialising the eb using the below commands
c:\dexter>eb init
Do you wish to continue with CodeCommit? (y/N) (default is n): n
Do you want to set up SSH for your instances?
(Y/n): n
21.create Django environment and will take some time
>
c:\dexter>eb create dexter-env
22.Ensure to see the output of the command
c:\dexter>eb status
Environment details for: dexter-env
Application name: dexter-application
Region: us-west-2
Deployed Version: app-190915_065124
Environment ID: e-8e2vwjhtd2
Platform: arn:aws:elasticbeanstalk:us-west-2::platform/Python 3.6 running on 64bit Amazon Linux/2.9.2
Tier: WebServer-Standard-1.0
CNAME: dexter-env.rqqqnthbr8.us-west-2.elasticbeanstalk.com
Updated: 2019–09–15 06:54:20.876000+00:00
Status: Ready
Health: Green
Stay with me we are almost done
23.Open the file settings.py inside folder apollo and find for the ALLOWED_HOSTS and add
dexter-env.rqqqnthbr8.us-west-2.elasticbeanstalk.com(highlighted above) and save it.
24.Now finally
c:\dexter>eb deploy
Creating application version archive “app-190915_071354”.
Uploading dexter-application/app-190915_071354.zip to S3. This may take a while.
Upload Complete.
2019–09–15 07:13:59 INFO Environment update is starting.
2019–09–15 07:14:03 INFO Deploying new version to instance(s).
2019–09–15 07:14:24 INFO New application version was deployed to running EC2 instances.
2019–09–15 07:14:24 INFO Environment update completed successfully.
25.You may check the application using
c:\dexter>eb open
the above will open the application like below
Finally we have deployed the application to EBS of Aws and You may see the status of application in console like the below
Note: If you are using your personal account to learn Kindly ensure to terminate EC2 and EBS environment once after you try.
Happy Learning and you may motivate me by your claps.
Queries and Comments are welcome always.
You may follow me at
https://www.pylabz.com/2019/09/deploy-django-application-to-aws-cloud.html