Deploying Django app to an AWS EC2 instance can be perceived as complex.

A lot of that complexity comes from the tricky process of creating an instance, access keys and not user friendly AWS interface.

This article will cover how to deploy your Django app to the AWS EC2 instance.

While AWS rightfully perceived as quite a tricky service to set up, if you follow this tutorial, you should have your app running on an EC2 instance in like 15 minutes.

If you are looking for tutorial for another cloud provider checkout out one of these:

Table of contents

Requirements

  1. An application to deploy
  2. AWS Account
  3. Appliku Account

Django Application

While this article's focus is on deploying Django apps on AWS EC2, the deployment steps are applicable to any Python application too.

For the easiest deployment your app needs to:

  • have requirements.txt file in the root of the git repository listing all dependencies. Don't forget to add gunicorn and psycopg2-binary==2.9.5 or higher
  • the Procfile in the root of the git repository listing all the commands to run your app. You can see an example from the SpeedPy boilerplate here: https://gitlab.com/speedpycom/speedpycom-backend/-/blob/main/Procfile
  • Your app needs to respect environment variables like DATABASE_URL for the database credentials, DJANGO_ALLOWED_HOSTS for the list of allowed domains, DJANGO_SECRET_KEY and REDIS_URL if applicable.
  • Please, don't forget to have DEBUG turned off when you deploy to a publicly accessible environment.

We have a full Django with Docker and Postgres Tutorial if you want to learn more, or you can check our SpeedPy.com SaaS boilerplate

AWS Account for Deploying to EC2

If you don't already have an AWS account, go to https://portal.aws.amazon.com/billing/signup to sign up.

Then go here to create a user that Appliku will use to perform server setup: https://console.aws.amazon.com/iam/home?#/users

Fill in the username, leave "Provide user access to the AWS Management Console" unchecked, click next.

On the next page switch to the tab "Attach Existing Policies Directly".

Add this policy:

AmazonEC2FullAccess

Then click the "next" button.

Click "Create user"

You will be taken to the list of users.

Click on the username of the newly created user.

Go to the tab "Security Credentials"

Click on the "Create Access Key" button.

Select "Thrid-party service", check the checkbox and click "Next", on the tag page leave it empty and click "Create Access Key"

Copy the credentials to the safe space!

Appliku Account

If you don't already have an Appliku Account create it by going here: https://app.appliku.com/

Click on "Add a server"

Pick "Select Credentials" for AWS provider.

Paste your AWS access key and secret key and click "Test & Save Credentials"

If everything if fine, then "Status" must become active

Go to "Servers"

Then "Add Server"

You can now select the "AWS" provider

Select the region, instance type and the disk size. Keep in mind that to fit under the limits of free tier, the disk size must be under 30GB.

Click "Create EC2 Instance"

You will see the EC2 information page, which is empty for now. Appliku is waiting for AWS to provide us the server. This can take up to a minute.

After that Appliku will begin setting up the AWS EC2 instance for you installing all the required packages.

You can see that from the "Setup Status": "Started". You can also see the Setup Logs.

Server setup takes a few minutes. I recommend to stay here and wait until it finishes.

Sometimes cloud providers give a broken EC2 instance and setup fails. In this case you need to go to AWS console (the link "Manage Server in AWS Panel" will bring you straight to the needed page) and select the ec2 instance and terminate it. Then start the process of adding an EC2 instance once again.

Server setup is complete!

Creating and Deploying Django Application to EC2 Instance

Click on the "Applications" menu link

You will get to the Git Provider selection.

Creating application from GitHub

Select the "Setup Credentials" for GitHub.

You need to get a personal token from GitHub.

Go to this link to create a token: https://github.com/settings/tokens

Click on Generate new token -> Generate new token(Classic) Fill in the note, select expiration date, check the "repo" checkbox and "user:email"

And click "Generate token".

Copy the token from the next page.

Go back to Appliku dashboard and paste the GitHub Token, and click Test Credentials and Save

Status must change to "Active".

Click on the logo in top left corner to get back to your team and go to Applications again and create an app, select GitHub.

Fill the form with the app name, select the repository with your application, branch and select the server, click on "Create Application".

You will see your app has been created, but not yet deployed.

Creating application from GitLab

Alternatively if you use GitLab you should add credentials for GitLab and create an app from GitLab.

Go to Applications menu link, Click "Add Application" and Setup credentials for GitLab.

You need to create a Personal Access Token here: https://gitlab.com/-/profile/personal_access_tokens

Give the key a name, set or clear expiration date, select "api" checkbox and click "Create Personal Access token"

Copy the token and paste it in Appliku dashboard.

After you click "Test Credentials and Save" the Status should turn green and say "Active".

Click on the logo to go back to your team, go to Applications, Add Application and select GitLab

Give your application a name, pick repository and the branch, select the server and click "Create Application"

Setup the app

Databases

If your application needs a database, which Django apps need most of the time, you can click on the "Manage" in databases block and "Create Database"

Select a database type you need and your server to deploy the DB to, click "Create database"

Your database will start deploying, wait for the deployment status to become "deployed".

Go back to your app

You can see your database is in the list.

Environment variables and build settings

Click on the "Settings" link next to the application name.

Go to the "Environment Variables" tab

You can see that the DATABASE_URL is already added automatically from the database you created.

If you have a .env file you can use "Import from .env" button to paste variables.

Or click on "Add" button and add variables one by one.

Usually for Django app we need to add DJANGO_ALLOWED_HOSTS and DJANGO_SECRET_KEY.

Appliku provides a default host name for your application which is YOUR_APP_NAME+ '.applikuapp.com'

After you finished adding your variables click on "Save and Deploy"

You will see that the deployment started.

Go back to the application overview.

The "Deploy Now" button is grey because deployment is in progress and in the "Latest deployment" Block you can see the currently running deployment.

Click on "Deployment #1"

You can see the deployment log. Scroll down to see the most recent output.

After it finishes and reports the "exit_code: 0", which is success go back to your application overview

Click on "View Application"

It will open your application in a new window!

Congratulations. You have deployed a Django application to an AWS EC2 instance!