How to Deploy Your Django App to AWS 2023

How to Deploy Your Django App to AWS: Deploying a Django application to AWS can seem daunting at first, but following this beginner’s guide will walk you through the entire process step-by-step. By the end, you’ll have your Django app up and running on AWS!

Prerequisites

Before we begin, make sure you have the following:

  • A Django application that you want to deploy. It should be tested locally and ready for production.
  • An AWS account with proper permissions to create resources like EC2 instances, load balancers, etc.
  • AWS CLI installed and configured on your local machine.
  • Basic understanding of AWS services like EC2, VPC, ELB, RDS, etc. Don’t worry if you’re new to AWS, I’ll explain each service as we go through the steps.

With the prerequisites covered, let’s get started!

Set Up AWS VPC and Subnets

The first thing we need to do is set up a Virtual Private Cloud (VPC) on AWS to host our Django application. A VPC is like a virtual network dedicated to your AWS account.

Go to the VPC dashboard on AWS and click “Create VPC”. Name your VPC (eg. myapp-vpc) and leave other settings as default. This will create a VPC with an internet gateway to allow public traffic.

Next, create 2 public subnets in different availability zones under this VPC – one for web servers and one for the load balancer. Also create a private subnet for the database.

Make a note of the subnet IDs as we’ll need them later.

People Also Read – Best AI Tools for Data Analysis

Create EC2 Instances

With the VPC and subnets ready, we can now launch EC2 instances which will host our Django app.

Go to the EC2 dashboard and launch two EC2 instances in the web server public subnet. Use a Linux AMI like Ubuntu or Amazon Linux. Make sure to allow HTTP traffic in the security group.

Also launch an EC2 instance in the private subnet for the database. This doesn’t need any inbound internet access.

Once the instances are up and running, note their private IP addresses.

Install Dependencies

With the EC2 instances launched, let’s SSH into the 2 web server instances one by one and install dependencies:

sudo apt update
sudo apt install python3 python3-pip nginx git

Also install additional system packages required for your Django project like postgresql, mysql, etc.

Next, install virtualenv and create a virtual environment for the project:

pip3 install virtualenv 
virtualenv venv
source venv/bin/activate

Your EC2 web server instances are now ready to deploy code!

Set Up Load Balancer

Before we deploy Django, let’s set up an Application Load Balancer (ALB) to distribute traffic across the 2 web server instances.

Go to the EC2 Load Balancing dashboard and click Create Load Balancer. Choose Application Load Balancer.

Configure the ALB in the web server public subnet and assign the 2 web server instances to it. Specify HTTP port 80 in the listener.

Once the ALB is created note its DNS name (eg. myapp-alb-12345.us-east-1.elb.amazonaws.com)

We’ll point our Django domain name to this ALB after deploying the app.

Set Up RDS Database

Moving on, we need to create a database for our Django project. The easiest way is to use RDS (Relational Database Service).

Go to the RDS dashboard and launch a Postgres or MySQL instance in the private subnet. Make sure to enable public accessibility so web servers can reach it.

Note the database endpoint and master username/password. We’ll configure Django to use this database.

Deploy Django Code

We’ve set up the underlying infrastructure. Now let’s deploy our Django code.

On your local machine, compress your Django project folder into a .zip file and upload it to an S3 bucket.

Next, SSH into the first web server instance and run:

aws s3 cp s3://your-bucket/project.zip .
unzip project.zip
pip3 install -r requirements.txt

This will copy the project from S3, install app dependencies, and make it ready for deployment.

Update Django settings.py with the RDS database details, ALLOWED_HOSTS, and any other production configuration. Also collectstatic to copy static files.

Configure Nginx

We need a web server like Nginx to pass requests to the Django app. Here’s a basic Nginx config for Django:

server {
    listen 80;

    location / {
        proxy_pass http://127.0.0.1:8000;
    }

    location /static/ {
        autoindex on;    
        alias /path/to/project/static/; 
    }
}

Save this as /etc/nginx/sites-available/myapp and symlink it to sites-enabled.

Finally, restart Nginx and start the Django app:

sudo service nginx restart
cd project
gunicorn --bind 0.0.0.0:8000 config.wsgi

The Django app should now be accessible through the EC2 instance public IP!

Automate Deployments

Manually deploying like this can be cumbersome. Let’s automate it with Fabric, a Python automation library.

Create a fabfile.py with tasks to automate deployments. Example:

from fabric.api import *

env.hosts = ['ec2-host1','ec2-host2'] 

def deploy():
   # deploy code from S3
   # install dependencies
   # collectstatic etc

Now just run fab deploy to deploy changes to all hosts with one command!

You can also set up CI/CD pipelines with tools like Jenkins, CodeDeploy etc. But Fabric is a quick way to get started with automation.

Set Up Domain Name

Currently, the Django app is only accessible via the EC2 public IP. Let’s set up a custom domain name.

Purchase a domain name like myapp.com and create an A record pointing to the ALB DNS name. This will route traffic from the domain to the load balancer.

You can also get free subdomain names with Route 53. Either way, visitors can now access the app at your domain!

Job Notification Join us on Telegram: Click here

Secure with SSL Certificate

Finally, let’s add SSL encryption to our domain name using AWS Certificate Manager.

Go to Certificate Manager and request a public certificate for your domain name. Select the option to validate via DNS.

Once issued, edit your ALB listeners and add a new secure listener on port 443 using this certificate.

You can redirect all HTTP to HTTPS traffic too. Your Django app is now accessible and secure with HTTPS!

Conclusion

That wraps up our guide on How to Deploy Your Django App to AWS Here are the key steps we went through:

  1. Creating a VPC with public and private subnets
  2. Launching EC2 instances for web servers and database
  3. Setting up an Application Load Balancer
  4. Creating an RDS database instance
  5. Deploying app code from S3 and configuring Nginx
  6. Automating deployments with Fabric
  7. Setting up a custom domain name and SSL certificate

Following these steps, you can reliably host your Django app on AWS and scale it based on incoming traffic.

Some additional ideas for taking this further:

  • Using S3 for media storage
  • Enabling CloudWatch monitoring
  • Migrating database to Aurora
  • Setting up autoscaling groups

Hope this gives you a good foundation for deploying Django apps on AWS! Let me know if you have any other questions.

Leave a comment