How to Develop a Django Web App on AWS Cloud9

Jan. 5, 2021, 4:39 p.m.
Django AWS · 6 min read
How to Develop a Django Web App on AWS Cloud9

Amazon Web Services offers an online integrated development server (IDE) so you can develop, debug, and run your code with just a browser. 

This service is called AWS Cloud9 and like almost everything else on AWS, you only pay for what you need. 

Compute and Storage fees are the only charges and will run you about $2.00 USD if you use Cloud9 for around 4 hours a day for 20 days.

This article is a complete walkthrough of how to set up the default AWS Cloud9 settings to develop a Django, a Python-based web framework, project.

  1. How to create an AWS account
  2. Locating the Cloud9 service
  3. Creating a new Cloud9 environment
  4. Getting started with AWS Cloud9
  5. How to create a virtual environment in Cloud9
  6. Installing Django web framework
  7. Creating a Django project and web app
  8. How to run the Cloud9 server
  9. Django error when trying to run the Cloud9 server
  10. Deploying from Cloud9 to AWS EB

 


 

How to create an AWS account

AWS sign up

To begin, you will need to create an AWS account. The process is straightforward and you will only be charged for the services you use on AWS. 

 


 

Locating the Cloud9 service

AWS search for Cloud9

Once signed up, click on the search bar and search for the "Cloud9" service.

 

Or scroll down to the Developer Tools section under All Services and click "Cloud9".

AWS find Cloud9 service

Click on "Cloud9" to go to the Cloud9 homepage.

 


 

Creating a new Cloud9 environment

AWS Cloud9 Homepage

AWS Cloud9 homepage

Once on the Cloud9 homepage, click on the orange button "Create environment".

 

Step 1: Name environment

AWS Cloud9 create environment name

You are prompted to enter the name of your environment.

We recommend you name the environment something comprehensive so you can remember what's in it.

You can also choose to add a description if you so choose.

 

Step 2: Configuration settings

AWS Cloud9 Connfigurations

Next are the environment configuration settings.

For Environment type, leave "Create a new EC2 instance for the environment (direct access)" selected.

For Instance type, leave it at "t2.micro (1 GiB RAM +1 vCPU)". This is the most cost-effective instance if you are just getting started.

For Platform, change the setting to "Ubuntu Server 18.04 LTS". However, feel free to use Linux if you are more familiar with that operating system.

Finally, for Cost-saving settings,  leave the amount at the default "After 30 minutes". This means auto-hibernate will apply after 30 minutes of inactivity to prevent you from being unnecessarily charged for leaving the server running. 

 

Step 3: Review

Review the environment name and configurations, then click the button "Create environment" when you are ready.

 


 

Getting started with AWS Cloud9

AWS Cloud9 Creating a new environment

AWS Cloud9 creating AWS env

It will take a few minutes for the environment to finish the setup.

From there you will see two tabs a "Welcome" tab and an "AWS Toolkit - Quick Start". 

Both provide helpful information on customizing AWS Cloud9.

Feel free to read these documents as we will just be reviewing the basics needed to get a Django project up and running.

 

AWS Cloud9 IDE

AWS Cloud9 IDE

What is included in AWS Cloud 9?

  • code editor
  • terminal
  • debugger
  • file tree

 


 

How to create a virtual environment in Cloud9

AWS Cloud9 Terminal

user:~/environment $ python3 -m venv env
user:~/environment $ cd env
user:~/environment/env $ source bin/activate
(env) user:~/environment/env $

To create a virtual environment in Cloud9 named env:

Create a virtual environment.

python3 -m venv env 

Enter into the virtual environment folder.

cd env

And activate the virtual environment.

source bin/activate

 

If you would like to know more about virtual environments visit, Creating a Python Virtual Environment for Beginners.

 


 

Install Django web framework

AWS Cloud9 Terminal

(env) user:~/environment/env $ pip install django==2.1.15

You must install a version of Django 2 if you are planning on deploying with AWS Elastic Beanstalk.

We will be using AWS EB for this tutorial.

 


 

Create a Django project and web app

AWS Cloud9 Terminal

(env) user:~/environment/env $ django-admin startproject mysite
(env) user:~/environment/env $ cd mysite
(env) user:~/environment/env/mysite $ python3 manage.py startapp main

With Django installed, we can move on to creating the actual Django project and the app within it.

To create a project named mysite:

django-admin startproject mysite

To create the app:

cd mysite

python3 manage.py startapp main

 

For more explanation on Django installation and initial setup, visit Django Quick Start Installation.

 


 

How to run the Cloud9 Server

AWS Cloud9 Code Editor

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 2.1.15.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os

...

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    ...

Before running the server, we need to update the Django settings.py file with the allowed host. 

Go to the file tree on the left side of the IDE and double click env > mysite > mysite > settings.py.

The Django settings.py file will open in the Cloud9 code editor. It will look like the file above.

 

AWS Cloud9 Code Editor

ALLOWED_HOSTS = ['*']

Scroll down to the ALLOWED_HOSTS and list an asterisk within the brackets, allowing all hosts. Save the file. 

Alternatively, you can leave the ALLOWED_HOSTS blank, run the server, then grab the domain URL after.

 

AWS Cloud9 Terminal

(env) user:~/environment/env/mysite $ python3 manage.py runserver 8080

To run the Cloud9 server, you need to run the command python3 manage.py runserver 8080. This will create a Cloud9 URL for your development server. 

Upon running the command, a green box will appear with the link to where your code is running.

Click the link.

 

Django Rocket in a browser window

Django rocket on Cloud9

You should see the Django rocket. 

 


 

Django error when trying to run the Cloud9 server

If you are getting the Django yellow page error

Invalid HTTP_HOST header: 'abcd.vfs.cloud9.us-west-2.amazonaws.com'. You need to add 'abcd.vfs.cloud9.us-west-2.amazonaws.com' to ALLOWED_HOSTS.

 

AWS Cloud9 Code Editor

ALLOWED_HOSTS = ['abcd.vfs.cloud9.us-west-2.amazonaws.com']

Go back to the Django settings.py file and add the URL given in the Django error under ALLOWED_HOSTS. Save the file, reload the browser.

 


 

Deploying Django from Cloud9 to AWS EB

AWS Elastic Beanstalk Django deployment

AWS EB Django deployment

Once you have finished development, you can deploy your Django project on your cCloud9 environment to an AWS Elastic Beanstalk environment.

If you are looking to deploy your web app using the AWS Elastic Beanstalk Command Line Interface, follow the instructions provided in the course Building A Django Web App for more information. 

Or if you can choose to deploy directly from the AWS Elastic Beanstalk Console with your zipped project.

 






Post a Comment
Join the community

0 Comments