Thursday, December 10, 2015

Deploying a Django App to AWS Elastic Beanstalk


                                 
                                    Deploying a Django App to AWS Elastic Beanstalk

Introduction


Amazon Web Services (AWS) comprises dozens of services, each of which exposes an area of functionality. While the variety of services offers flexibility for how you want to manage your AWS infrastructure, it can be challenging to figure out which services to use and how to provision them.
With Elastic Beanstalk, you can quickly deploy and manage applications in the AWS cloud without worrying about the infrastructure that runs those applications. AWS Elastic Beanstalk reduces management complexity without restricting choice or control. You simply upload your application, and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring. 

The following steps is tell you  how to setup and deploy a Django application toAmazon Web Services (AWS) all while remaining sane.

Technology/Tool used
  • Python
  • Django
  • Amazon Elastic Beanstalk,EC2,S3 and RDS
  • EB CLI . 3 . X
  • Postgresql/mysql
Note: Amazon Elastic Beanstalk does not support Python 3 natively

Step 1 :
  • Create main root directory 
  • Create  a virtualenv [ (1)pip install virtualenv,virtualenv envname ]
  • Install packages using pip commands [pip install django==1.8.5]
  • Create a package using command (django-admin.py startproject main_website
  • Create a apps inside the main_website project using[python manage.py startapp home]
  • Install the apps in settings.py
  • Set the database according your needs
  •  Create a requirements.txt file inside the root directory [ pip freeze > requirements.txt]
  • add file .gitignore in root directory
Step 2 :
  • Install awsebcli in your machine (using pip , pip install awsebcli)
  • Configure aws in your machine with key and access id [ aws configure command in your machine it will ask you to enter the credentials , add the credentials then  aws will created ]
  • Locate the project root directory [ using cd command cd eb_main_website ]
Step 3 :
  • Test eb Installation is working fine [ eb --version ]
Step 4 :
  • Configure EB - Initialize project with aws 
  • Enter eb init
  • Your need to enter the Credentials,Application name,Python version,SSH,RSA keypair
  • Once you enter the these data eb init will create a file in .elasticbeanstalk  in main directory , there you can see the file called config.yml
Project Structure

  -- eb_main_website [ Main Directory ]
   |
   |----.ebextensions
   |    |
   |   |---01-main_website.config
   |   |---02_python.config
   |
   |----.elasticbeanstalk
   | |
   | |---.config.yml
   | |---.iod-test.env.yml
   |
   |----main_website
   | |
   | |---apps
   | | |
   | | |---staffs
   | |---home
   | |   |---__init__.py
   | |   |
   | |   |---management
   | |   |
   | |   |---__init__.py
   | |   |
   | |   |---commands
   | |   |
   | |   |--- __init__.py
   | |   |
   | |   |--- createsu.py
   |    |
   |    |---main_website
   | |
   | |--- __init__.py
   | |
   | |--- settings.py
   | |
   | |--- urls.py
   | |
   | |--- wsgi.py
   |
   |----www
   | |
   | |--static
   |
   |---- .gitignore
   |
   |---- error.txt
   |
   |--- requirements.txt