Deploying Django with AWS Elastic Beanstalk!

Deploying Django with AWS Elastic Beanstalk!

Elastic Beanstalk is a major breakthrough when it comes to cloud platform. You can quickly deploy and manage django applications in the AWS Cloud without worrying about the infrastructure that runs those applications. You simply upload your application and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring. The following shows how we deploy a django application at SayOne:

Getting started

  • First you need an AWS account, sign up or login if you already have an AWS account.
  • Go to the Elastic Beanstalk management console to create a new application.
  • Give your application a name.
  • To deploy your django application, select the Web server environment.
  • Select the predefined python configuration. Environment type can be single instance for basic uses. Select load balancing for advanced uses.
  • Select the source of the application version. Let us use a sample application. You can either upload the code in zip format or you can upload it from the s3 bucket directly.
  • Next you can change the default environment name and URL if necessary.
  • You can create additional resources or create a new RDS database if required.
  • Select the required instance type where you want to deploy your django  application. Select a ssh key for the instance.
  • Review the information entered and launch the application. The launch could take some time depending upon your application.
  • A sample application is now deployed successfully.

To deploy the code to Elastic beanstalk via terminal, grab  the  application from git repository:

  • git clone https://github.com/django-ve/helloworld.git
  • Create a virtualenv and install the requirements via pip.

pip install -r requirements.txt

Test the app locally using: python manage.py runserver

Install the awsebcli package using pip: pip install awsebcli

Initialize your application: eb init or you can use eb init -i

To configure your environment enter the required details in the prompt.
Default Region: Choose a region closest to the end user.
Credentials: Enter the AWS Access key id and secret key
Application name: Select the application name, to create a new application eb  will use the directory name.
Python version: Ebcli automatically detects that your app is python and asks for a confirmation. Select the appropriate version.

  1. Python 3.4
  2. Python 2.7
  3. Python 3.4 (Preconfigured - Docker)

SSH: Select the SSH key required to SSH into your EC2 instance.


At this point eb creates a .elasticbeanstalk/config.yml file, which contains the application details of the Elastic Beanstalk

To install the required packages, create a file .ebextensions/packages.config

packages:
  yum:
    git: []
    postgresql93-devel: []
    libjpeg-turbo-devel: []

 Create a file .ebextensions/django.config to define the django settings module, wsgi path and static directory.You can also add the container commands for the environment in this file.

 container_commands:

  01_collectstatic:
    Command:"source /opt/python/run/venv/bin/activate && python helloworld/manage.py collectstatic --noinput"
  02_migrate:
  Command:"source /opt/python/run/venv/bin/activate && python helloworld/manage.py migrate --noinput"
  leader_only: true

 

option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "helloworld.settings"
  "aws:elasticbeanstalk:container:python":
    WSGIPath: "helloworld/wsgi.py"
  "aws:elasticbeanstalk:container:python:staticfiles":
    "/static/": "static/"

You can define database credentials as environment variables.    

if 'RDS_HOSTNAME' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }

 Commit the changes and the new directory to the git repository, the eb deploy works off the latest commit, and will thus only be aware of our file changes after we commit them to git

git add .ebextensions/

git commit -m "added eb package configuration"

To deploy our application to the Elastic beanstalk

 eb deploy

You can define the database environment variables in the configuration settings of the elastic beanstalk console.


Now you have successfully updated the code in Elastic Beanstalk environment. Further you can simply update the code by using “eb deploy”. The autoscaling environment will take care of the rest of the deployment process. 

Read the complete blog at Sayone Blog

要查看或添加评论,请登录

Renjith Raj的更多文章

社区洞察

其他会员也浏览了