Deploying a Django site on AWS Elastic Beanstalk

Deploying a Django site on AWS Elastic Beanstalk

While a Django site can be set up locally with ease, making it accessible everywhere takes a substantial amount of effort. Ranging from infrastructure management to network configurations, these steps are complex, error-prone and time-consuming for developers and businesses, and hence the showstoppers to web application deployment.

AWS Elastic Beanstalk is a managed service for deploying and scaling web applications and services. Apart from capacity provisioning and load balancing, Elastic Beanstalk also provides built-in auto-scaling, monitoring and logging capabilities, enhancing the application reliability and availability.

Prerequisites

Git
Python 3
pip
virtualenv

Configuring the Django project for Elastic Beanstalk

Change into the outer djangoproj directory, if needed.

cd djangoproj

Get the required packages for the project.

pip freeze > requirements.txt

Create a directory named .ebextensions.

mkdir .ebextensions

Create a configuration file named django.config in the .ebextensions directory with the following contents.

option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: djangoproj.wsgi:application

Note: The above WSGIPath value refers to the application callable in the wsgi.py of the djangoproj project.

The following shows an expected directory structure after the configuration. You may have more directories if you have created or installed other applications.

djangoproj/
.ebextensions/
django.config
djangoproj/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
db.sqlite3
manage.py
requirements.txt

Installing the EB CLI

Note: It is recommended to complete the installation on a separate terminal so that the changes would not affect the code for the Django project.

Clone the setup scripts.

git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git

Install/Upgrade the EB CLI.

Unix: python ./aws-elastic-beanstalk-cli-setup/scripts/ebcli_installer.py
Windows: python .aws-elastic-beanstalk-cli-setupscriptsebcli_installer.py

If the output contains an instruction to add the EB CLI (and Python) executable files to the shell’s $PATH variable, follow it and run the command. You also need to restart the opened terminal(s) before running any eb commands there.

Optionally, you can clean up the cloned setup scripts.

Configuring the EB CLI

Run eb init and follow the prompt instructions.

Enter the number that corresponds to the desired region.

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)

(default is 3):

Enter the access key and secret key.

Note: The credentials are used by the EB CLI for managing Elastic Beanstalk applications. If you do not have the keys, follow Manage access keys to create one.

You have not yet set up your credentials or your credentials are incorrect.
You must provide your credentials.
(aws-access-id):
(aws-secret-key):

Enter an Elastic Beanstalk application name.

Enter Application Name
(default is “djangoproj”):

Enter Y to confirm Python as the platform.

It appears you are using Python. Is this correct?
(Y/n):

Enter 1 to select Python 3.11 as the platform branch.

Select a platform branch.
1) Python 3.11 running on 64bit Amazon Linux 2023
2) Python 3.9 running on 64bit Amazon Linux 2023
3) Python 3.8 running on 64bit Amazon Linux 2
4) Python 3.7 running on 64bit Amazon Linux 2 (Deprecated)
(default is 1):

Ignore the message related to CodeCommit setup.

Cannot setup CodeCommit because there is no Source Control setup, continuing with initialization

Note: Alternatively, the EB CLI provides integration with Git. For details, please refer to Using the EB CLI with Git.

Enter Y to set up SSH connection to any instances in the Elastic Beanstalk environment.

Do you want to set up SSH for your instances?
(Y/n):

Note: This connection allows you to inspect logs or to check other metrics on the instances (to be created in the next step) for easier troubleshooting.

Enter a SSH key pair name and enter a passphrase.

Type a keypair name.
(Default is aws-eb):

Note: The EB CLI registers this key pair with the instances and stores the private key in the local folder named .ssh in the user directory.

In the Elastic Beanstalk console, you should see the newly created application is listed on the Applications page.

Deploying the Django site

Create an Elastic Beanstalk environment named djangoproj-dev.

eb create djangoproj-dev

Note: The load-balanced environment is created under the application created in the previous step. The Django site will also be deployed to the this environment. The entire creation takes several minutes.

Follow the next steps only after the environment creation completes.

Get the domain name of the environment.

eb status

Environment details for: djangoproj-dev
Application name: djangoproj

CNAME: djangoproj-dev.eba-ttkddb9r.us-east-1.elasticbeanstalk.com

Note: The domain name refers to the above CNAME value.

The domain name can be also obtained from the Environments page.

You may notice the Health value is Red from the terminal or Severe from the console. This issue will be fixed after completing this section.

Append the domain name to the ALLOWED_HOSTS list in the file named settings.py in the ebdjango directory, and save the file.

ALLOWED_HOSTS = [“djangoproj-dev.eba-ttkddb9r.us-east-1.elasticbeanstalk.com”]

Re-deploy the Django site.

eb deploy

After the environment update completes, the Django site is deployed successfully with Elastic Beanstalk. You will see the following screen after navigating to the domain name above. Alternatively, this can be achieved by running eb open.

References

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html
https://github.com/aws/aws-elastic-beanstalk-cli-setup
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-configuration.html

Leave a Reply

Your email address will not be published. Required fields are marked *