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.

 

AWS

 

  • Go to the Elastic Beanstalk management console to create a new application.

 

Welcome

 

  • Give your application a name.

 

App info

 

  • To deploy your Django application, select the Web server environment.

 

Select web server environment

 

  • Select the predefined Python configuration. Environment type can be single instance for basic uses. Select load balancing for advanced uses.

 

Environment type

 

  • 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.

 

App version

 

  • Next you can change the default environment name and URL if necessary. 

 

Environment info

 

  • You can create additional resources or create a new RDS database if required.

 

Additional resources

 

  • Select the required instance type where you want to deploy your Django  application. Select an ssh key for the instance.

 

Config details

 

  • Review the information entered and launch the application. The launch could take some time depending upon your application.

 

Review

 

All apps

 

  • A sample application is now deployed successfully.

 

Success

 

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

 

All apps

 

You can define the database environment variables in the configuration settings of the Elastic Beanstalk console.

 

Apps

 

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. 

Subscribe to our newsletter. Get updates on awesome happenings in the technology world!
Newsletter subscription successfull! Something went wrong! Email is already registered!

TAGS