Deploying Django project on Elastic Beanstalk

Deploying a Django App to AWS Elastic Beanstalk

This section focuses on how to setup and deploy a Django application to Amazon Web Services(AWS).

Tools/technologies used:

    Python v2.7

    Django v1.7

    Amazon Elastic Beanstalk, EC2, and S3

NOTE: If you’re new to Elastic Beanstalk, check out their tutorial to help you get a Django 1.4 site up on AWS quite quickly.

Elastic Beanstalk:

Elastic Beanstalk is a Platform As A Service (PaaS) that streamlines the setup, deployment, and maintenance of your app on Amazon AWS. It’s a managed service, coupling the server (EC2), database (RDS), and your static files (S3). 

You can quickly deploy and manage your application, which automatically scales as your site grows. Check out the official documentation for more information here.

To actually start using Elastic Beanstalk you will need an account with AWS.

Elastic Beanstalk needs the following information to deploy an application:

    1. AWS access key ID - Your amazon access key ID.

    2. AWS secret key - Your amazon secret Key.

    3. Service region - the region where you want to deploy an application.

    Ex: US West (Oregon).

    4. Application Name - The name of the application. Ex: HelloWorld

    5. S3 Bucket - The bucket that beanstalk apps will be stored in.

    6. Environment name - A unique name for the deployment environment.

    Ex: Defaults to HelloWorld-env

    7. Solution stack - The solution stack specifies the operating system, architecture, and application server for a configuration template.

Procedure for deploying a Django Application :

1. Connect to the Region: Choosing the region closest to your end users will generally provide the best performance.

import boto
from boto.beanstalk import *

connection = connect_to_region(region_name, aws_access_key_id=aws_access_key_id, 

2. Create an application that has one configuration template named default and no application versions.

    a. application_name: The name of the application. 

        Constraint: This name must be unique within your account.

    b. description: Describes the application.

import boto
from boto.beanstalk import *

response = connection.create_application(application_name="first-app", 
                description="This is my first Elastic beanstalk Application")

3. Connect to S3:

from boto.s3.connection import S3Connection
s3_connection = S3Connection("aws_access_key_id", “aws_secret_access_key")

4. Upload an application zip file (archive version) to the s3 bucket.

from boto.s3.key import Key

bucket = s3_connection.get_bucket(bucket_name="beanstalk")

k = Key(bucket)
k.key = key

response = k.set_contents_from_filename("")

5. Create an application version for the application using create_application_version API:

    a. application_name: The name of the application.

    b. version_label: A label identifying this version. 

        Constraint: Must be unique per application.

    c. description: Describes this version.

    d. s3_bucket: The Amazon S3 bucket where the data is located.

    e. s3_key: The Amazon S3 key where the data is located.  Both s3_bucket and s3_key must be specified in order to use a specific source bundle.  

    NOTE: If both of these values are not specified the sample application will be used.

    f. auto_create_application:  true: Automatically creates the specified application for this version if it does not already exist.

response = connection.create_application_version(application_name="first-app", 
                s3_bucket="beanstalk", version_label="firstapp Release 0.0", 
                s3_key="firstapp Release 0.0", description="Release 0.0-Elastic BeanstalkApp")

6. Create an Environment using create_environment API: Launches an environment for the application using a configuration.

    NOTE: With Elastic Beanstalk, an application can have multiple environments development, testing, staging, production.

    a. application_name: The name of the application that contains the version to be deployed. 

    b. environment_name: A unique name for the deployment environment. Used in the application URL. 

    c. version_label: The name of the application version to deploy. If not specified, AWS
    Elastic Beanstalk attempts to launch the most recently created application version.

    d. template_name: The name of the configuration template to use in deployment.

    NOTE: You must specify either this (template_name) parameter or a SolutionStackName, but not both.

    e. solution_stack_name: This is an alternative to specifying a configuration name.

    f. cname_prefix: If specified, the environment attempts to use this value as the prefix for the CNAME. If not specified, the environment uses the environment name.

    NOTE : When you deploy an app to Elastic Beanstalk you will automatically get a domain name like DNS CNAME prefix is what you want to be used in place of xxx. Just go with the default.

    g. description: Describes this environment.

    h. option_settings :  Each element in the list is a tuple of (Namespace, OptionName, Value), for example:   [('aws:autoscaling:launchconfiguration', 'Ec2KeyName', 'mykeypair')]

    i. tier_name: The name of the tier.  Valid values are "WebServer" and "Worker". Defaults to "WebServer".

        The possible combinations are:

            * "WebServer" and "Standard" (the default)
            * "Worker" and "SQS/HTTP"

    j. tier_type: The type of the tier.  Valid values are "Standard" and "SQS/HTTP"

    k. tier_version: The version of the tier.  Valid values currently are "1.0". Defaults to "1.0".

solution_stack_name = "64bit Amazon Linux 2014.09 v1.2.0 running Python 2.7"
cname_prefix = "stage"
env_description = "Staging Environment"

# Each element in the list is a tuple of (Namespace, OptionName, Value)
option_settings = [
    ("aws:elasticbeanstalk:application:environment", "DJANGO_SETTINGS_MODULE", "Django_Fabric_ElasticBeanstalk.settings"),
    ("aws:elasticbeanstalk:application:environment", "application_stage", "staging"),
    ("aws:elasticbeanstalk:container:python", "WSGIPath", "Django_Fabric_ElasticBeanstalk/"),
    ("aws:elasticbeanstalk:container:python", "NumProcesses", 3),
    ("aws:elasticbeanstalk:container:python", "NumThreads", 20),
    ("aws:elasticbeanstalk:container:python:staticfiles", "/static/", "static/"),

tier_type = "Standard"
tier_name = "WebServer"
tier_version = "1.0"

response = connection.create_environment(application_name=app_name, 
                environment_name="stage-env", version_label="firstapp Release 0.0",
                solution_stack_name=solution_stack_name, cname_prefix=cname_prefix,
                description=env_description, option_settings=option_settings,
                tier_type=tier_type, tier_name=tier_name, tier_version=tier_version

7. Update the environment to deploy the new application version using update_environment.

response = connection.update_environment(environment_name="stage-env", 
                                version_label="firstapp Release 0.0")

That's it, I guess now you can manage to deploy django project on Elastic Beanstalk. If require any support regarding AWS Services then contact our AWS Consulting services to grow your business.

Posted On 22 January 2016 By MicroPyramid

Need any Help in your Project?Let's Talk

Latest Comments
AWS Lambda - Best Practices

After Years of Developing Lambda Scripts from creating Serverless Applications to Pipelining your Tasks, Here are the Best Practices that we follow.

Continue Reading...
Autoscaling Application with AutoScaling Groups and AWS LoadBalancer

Autoscaling Application with AutoScaling Groups and AWS LoadBalancer

Continue Reading...
How to Build and verify an application using aws codepipeline and creating custom events with lambda

How to build and verify an application using aws codepipeline and creating custom events with lambda.

Continue Reading...
AWS tips and tricks to optimize cost and performance for better ROI

Best Practices of AWS cost and Performance Optimization

Continue Reading...
How to process message queuing system by amazon SQS

How to process attributes of message queuing system by amazon SQS using Boto3

Continue Reading...
Easy and Fast way to implement AWS Lambda service

We are going to use a simple application called Gordan to prevent creating a lambda function and triggering actions which involves time taking and repetitive …

Continue Reading...
How To Send And Receive Email With Django And Amazon SES

django-ses-gateway a pluggable Django application is used for sending mails from your verified domains and verified emails. We can also use django-ses-gateway for receiving messages …

Continue Reading...
Deploy Django using CloudFormation Template

CloudFormation helps in Using JSON templates to describe the resources needed from aws. With this approach, we don't have to repeat the same manual configuration …

Continue Reading...
How to Mount S3 Bucket on Local Disk

It all starts with FUSE, FUSE is File System User Space. Operating Systems have Kernel Space and User Space. Kernel Space is where low level …

Continue Reading...
Using AWS Lambda with S3 and DynamoDB

AWS lambda is handy tool for event driven computation, here we will learn how to configure and setup lambda function so to run our function …

Continue Reading...
How to access EC2 instance even if pem file is lost

Accessing the EC2 instance even if you loose the pem file is rather easy.

1. First, create a new instance by creating new access …

Continue Reading...
Deploying Django project on Elastic Beanstalk

Here You can learn about how to setup and deploy a Django application to Amazon Web Services (AWS).

Tools/technologies used:
Python v2.7
Django v1.7
Amazon …

Continue Reading...
how to setup custom domain for amazon cloudfront

We all want our own domain name to be setup for cloud front instead of amazon default cloud front domain name. We need two things …

Continue Reading...
Paginating S3 objects using boto3

When using Boto you can only List 1000 objects per request. So to obtain all the objects in the bucket. You can use s3's paginator.

Continue Reading...
Creating Elastic Search Cluster (EC2, cloud-aws Plugin)

While handling Large amounts of data with elasticsearch, you may run out of server capacity or compute power, Forming a Elasticsearch cluster will reduce the …

Continue Reading...
Configuring and Testing Load Balancer in AWS EC2

When You have an application that is serving Huge Customer Base, so will be your Traffic. Sometimes The Application simply stops responding. We can use …

Continue Reading...
Django Hosting on Amazon EC2 with wordpress on same domain

Configuring the Wordpress as subdirectory can be tricky. In this tutorial we will Setup a Django Website alongside a wordpress blog.

Continue Reading...
Amazon SES - Handling Bounces and Complaints

In general while sending emails, we will prepare some recipient addresses as our mailing list, which are valid and our recipients want and expect our …

Continue Reading...
Amazon AWS IAM Roles and Policies

When You want to Provide access to Amazon Web Services Console or if you're planning to provide REST API Keys to your Developers of a …

Continue Reading...
CORS with Amazon S3 and CloudFront

We struggle to load fonts from CloudFront because of CORS.

CORS - Cross Origin Resource Sharing is a security measure to block macious scripts or …

Continue Reading...

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties