How to deploy a Django app on Elastic Beanstalk

Key takeaways:

  • AWS Elastic Beanstalk streamlines the process of deploying Django applications to the AWS Cloud. It handles infrastructure provisioning, configuration, and deployment details, allowing developers to focus on application development.

  • Elastic Beanstalk provides a managed environment with pre-configured resources, enabling easy scaling based on application traffic. It can automatically adjust the number of instances based on workload, ensuring efficient resource utilization.

  • While offering a fully managed environment, Elastic Beanstalk allows developers to customize configurations for finer control over the underlying infrastructure. Deployment can be done using the AWS Management Console, CLI, or API, providing flexibility in the deployment process.

AWS Elastic Beanstalk is a fully managed service that streamlines the process of launching and maintaining applications on the AWS Cloud. It offers a Platform as a Service (PaaS) solution, which removes the intricacies of managing infrastructure, enabling developers to concentrate on creating and deploying applications.

Elastic Beanstalk
Elastic Beanstalk

Application deployment and management with AWS Elastic Beanstalk

AWS Elastic Beanstalk is a service that simplifies the process of deploying applications by automatically handling deployment details, such as provisioning and configuring AWS resources. It provides managed environments with pre-configured resources, making it easy to scale based on demand. The service can automatically adjust the number of EC2 instances based on application traffic, ensuring efficient handling of varying workloads.

Elastic Beanstalk integrates with other AWS services, facilitating seamless utilization of a range of AWS offerings. It supports rolling updates for applications, allowing for zero-downtime deployments, and provides monitoring capabilities through AWS CloudWatch.

The service ensures a secure environment with features like encryption, AWS IAM integration, and compliance with industry standards. While it offers a fully managed environment, developers also have the option to customize configurations for more control over the underlying infrastructure. Elastic Beanstalk can be interacted with using the AWS Management Console, AWS CLI, or API, providing flexibility in deployment and management options.

Supported platforms

Elastic Beanstalk supports multiple programming languages and frameworks, including Python, Java, Node.js, Ruby, Go, PHP, Docker, and more. It accommodates various application architectures.

Deploying a Django application on Elastic Beanstalk

In this section, we will explore how to create a basic Django application and how we can deploy it on Elastic Beanstalk. We will use CLI commands to deploy our Django Application on Elastic Beanstalk. To deploy an application using CLI commands, we need Python 3.7 or later, awscli, virtualenv and awsebcli. We can download virtualenv by using the command below:

pip3 install virtualenv

Create a virtual environment

We can create a virtual environment by using the following command:

virtualenv ~/eb-virt
Create virtual environment

Activate the environment

We can activate the environment by using the following command:

source ~/eb-virt/bin/activate
Activate the virtual environment

We can check the Python version by the following command:

python3 --version
Check Python version

Now, we need to install the awsebcli, you can download it by using the following command:

pip3 install awsebcli

Once the awscli is installed, we can configure a session with AWS using the following command:

aws configure set default.region "<Your_AWS_Region>" && aws configure set aws_access_key_id "<Your_AWS_ACCESS_KEY>" && aws configure set aws_secret_access_key "<Your_AWS_SECRET_ACCESS_KEY>" && aws configure set default.output json

In the command above, replace <Your_AWS_Region> placeholder with the AWS region you are working in. Replace the <Your_ACCESS_KEY> and <Your_SECRET_ACCESS_KEY> placeholders with your actual access key and secret access key. If you don’t have these keys, follow the steps in this documentation to generate the keys.

If the Python version is less, then 3.7 you need to update it. The next step is to install Django.

Install Django

We can install Django by using the following command:

pip3 install django==2.2
Install Django

Note: You can use the latest version of Django as well.

Create a Django project

We can create a basic Django project by using the following command:

django-admin startproject djangoEB
Create Django project

This command creates a standard Django site named djangoEB with the following directory structure:

File structure
File structure

Run application locally

We can run the application locally by using the following command:

cd djangoEB &&\
python3 manage.py runserver
Start the application

After executing this command, your application will be hosted at http://127.0.0.1:8000

We can close the application by pressing ctrl+c.

Create requirements.txt

We can create a requirements.txt file by using the following command:

pip3 freeze > requirements.txt
Create a requirements file

Elastic Beanstalk will use this requirements file to install all the required packages on the hosted server.

Create a configuration file

Here, we'll create a directory named .ebextensions , and inside this directory, we'll create a configuration file named as django.config. In Elastic Beanstalk, these files are necessary as Elastic Beanstalk looks for a application.py file to deploy the application, and in Django applications, we don’t have an application.py file. The .ebextensions is used by Elastic Beanstalk to identify a folder containing configuration files and scripts that extend and customize the behavior of the deployment process. Here, we will specify the location of the WSGI script that Elastic Beanstalk uses to start the application. We can follow the commands below to create the .ebextensions directory and the django.config files:

mkdir .ebextensions && cd .ebextensions && touch django.config
.ebextensions and django.config files

Paste the following text in the django.config file:

option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: djangoEB.wsgi:application
django.config

This setting specifies the location of the WSGI script. For the Amazon Linux AMI Python platform version, replace the value for WSGIPath with djangoEB/wsgi.py.

Deactivate the virtual environment

We can use the following command to deactivate the virtual environment:

deactivate
Deactivate virtual environment

Next, we’ll create our application environment and deploy our configured application with Elastic Beanstalk.

Initialize the Elastic Beanstalk repository

You can initialize the Elastic Beanstalk application by using the following command:

eb init -p <Python_Version> --region <YOUR_REGION> <Django_Application_Name>
Initialize repository

In the command above, replace the <Python_Version> placeholder with the version of Python compatible with the application, which would look like python-3.X, <YOUR_REGION> placeholder with your desired region and <Django_Application_Name> placeholder with the name of the application we want to deploy.

Note: We can also specify the Python version as the managed platform version that is supported by elastic beanstalk which looks like: Python 3.X running on 64bit Amazon Linux X. Here the X represents the the version, which may vary.

This command creates an application named <Django_Application_Name>. It also configures your local repository to create environments with the specified Python platform version.

Create an Elastic Beanstalk environment

You can use the following command to create an environment and deploy your application to it:

eb create django-env
Create Elastic Beanstalk environment

Executing this command generates a load-balanced Elastic Beanstalk environment named django-env. The environment creation process typically requires approximately 5 minutes. During this time, Elastic Beanstalk generates the necessary resources for your application’s execution, delivering informational messages to your terminal through the EB CLI.

Note: If you encounter a "service role required" error message, run eb create without specifying an environment name, and the EB CLI creates the role for you.

Once the environment creation process finishes, you can retrieve the domain name of your newly created environment by executing the command eb status. The domain name is available next to the CNAME tag in the response.

Update the Allowed hosts

Navigate to the settings.py file within the djangoEB directory. Find the ALLOWED_HOSTS setting, and append the domain name of your application, which you obtained in the prior step, to the existing value of the setting. The updated ALLOWED_HOSTS are shown below:

ALLOWED_HOSTS = ['<YOUR_EB_DOMAIN>']
Updated Allowed hosts

Replace the <YOUR_EB_DOMAIN> place holder with the domain name we retrieved earlier. Save the file, and we will deploy the application in the next step.

Deploy the application

We can deploy the application by using the following command:

eb deploy
Deploy application

When the environment update process completes, you can open your website by using the following command:

eb open
Access application

Try it yourself

In the playground “Django application on Elastic Beanstalk” below, we have a basic Django application. The .ebextensions directory and django.config file are already created for you. Follow these steps to deploy the application on Elastic Beanstalk:

  1. Initialize the Elastic Beanstalk application: Run the following command to initialize the application, specifying the Python version, AWS region, and application name:

eb init -p <Python_Version> --region <YOUR_REGION> <Django_Application_Name>

  For example:

eb init -p python-3.11 --region us-east-1 django-tutorial

This initializes an Elastic Beanstalk application named django-tutorial in the us-east-1 region with Python 3.11 as the platform.

  1. Create the Elastic Beanstalk environment: Once the application is initialized, create an environment using the following command:

eb create django-env
  1. Retrieve the application domain: After the environment is successfully created, retrieve the Elastic Beanstalk domain for your application by running:

eb status

  The response will include details about your environment, similar to this:

Environment details for: <Environment_Name>
Application name: <Application_Name>
Region: <Deployed_Region>
Deployed Version: <Version_ID>
Environment ID: <Environment_ID>
Platform: <Resource_ARN>
Tier: WebServer-Standard-1.0
CNAME: <Domain_Name>
Updated: <Date>
Status: Ready
Health: Green

Save the value of the CNAME field. This is the domain where your application will be hosted and accessible.

  1. The update allowed hosts in Django: Update the ALLOWED_HOSTS configuration in your ebdjango/settings.py file to allow access from your Elastic Beanstalk domain. Run the following command, replacing <CNAME> with the domain from the eb status output:

sed -i 's/<Your_EB_Domain>/<CNAME>/g' ebdjango/settings.py

  This ensures your application is accessible from the Elastic Beanstalk domain.

  1. Deploy the application: Deploy the updated application using:

eb deploy
option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application
Django application on Elastic Beanstalk

Visit the <CNAME> domain to access your deployed Django application.

If you want to learn more about Elastic Beanstalk, you can try out the following labs:

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Which server is best for Django?

The best server for your Django application depends on various factors, like your specific needs, budget, and technical expertise. Here are a few popular choices:

  • AWS (Amazon Web Services): Offers a wide range of services, including EC2 instances, Elastic Beanstalk, and Lambda functions. It’s highly scalable and customizable but requires more technical expertise.
  • DigitalOcean: Provides user-friendly virtual machines and managed databases. It’s a good option for smaller-scale applications and those who prefer a simpler setup.
  • Heroku: A platform as a service (PaaS) that simplifies deployment and management. It’s ideal for small to medium-sized applications and developers who want to focus on coding.
  • Google Cloud Platform (GCP): Offers a range of services, including App Engine, Compute Engine, and Cloud Functions. It’s a good choice for those who are already using other Google Cloud services.

Is AWS free for Django?

AWS offers a free tier that allows you to use some services, including EC2 instances, for a limited period. However, running a Django application on AWS for free indefinitely is not possible. You’ll eventually need to pay for the resources consumed.


Which server does Django run on?

Django itself doesn’t directly run on a specific server. It’s a Python framework that needs to be served by a web server. Common web servers used for deploying Django applications include:  

  • Gunicorn: A Python WSGI HTTP server, often used for production environments due to its performance and efficiency.
  • uWSGI: Another popular Python WSGI server, known for its flexibility and performance.
  • Apache: A widely-used web server that can serve Django applications through modules like mod_wsgi.
  • Nginx: A high-performance web server often used as a reverse proxy to distribute traffic to Gunicorn or uWSGI instances.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved