Deploy Django on AWS Elastic Beanstalk Linux 2 + PostgreSQL 12.5+ AWS S3 Part 1

Jenniferguayta
Allient
Published in
6 min readMay 4, 2021

--

AWS Diagram

Amazon provides two versions of Amazon Linux. Elastic Beanstalk has support for both Linux versions. This new generation has new features for python as:

  • Linux 2 have support for python 3.7 and 3.8
  • Some software packages might not be available on Amazon Linux 2, or their names might have changed.
  • Some platform-specific configuration options have moved from their platform-specific namespaces to different.
  • All Amazon Linux 2 platform versions use Nginx as their default reverse proxy server.
  • Gunicorn is the default WSGI server.
  • Path to application’s directory on Amazon EC2 instances of your environment is /var/app/current

With this background it’s easy to assume that deploy steps and setting up will change. In this post, we’re going to deploy our Django project as a tester and connect it with PostgresSQL database storage in AWS RDS.

Prerequisites

  • Install awsebcli python library pip install
pip install awsebcli

Clone project and directory

I am going to use an API test code for making this tutorial the link you can found here. The Django project is in the backend folder as you can see in the schema.

REST-API
|_backend
|__ .ebextensions
|__ apps
|___ app
|___ authentication
|___ media
|___ news
|__ backend
|__ Procfile
|__ manage.py
|__ requirements.txt
|_env
|_.env
|_gitignore
|_LICENSE
|_readme.md

Consideration before deploying

1.Have a requirements.txt with the python libraries. Our example already has this file but you can get it with this command in your python virtual environment.

pip freeze > requirements.txt

Note: If you are using windows please verify that your file doesn’t have libraries used for windows as pywin32

Note: In order to avoid problems with psycopg2 library install only psycopg2-binary

2. The project must be in production mode. You should check these options on your settings.py

  • Set Debug in False
  • Set CSRF_COOKIE_SECURE in True
  • Set SESSION_COOKIE_SECURE in True

If your application needs CORS, you should deactivate it for all origins.

  • Set CORS_ORIGIN_ALLOW_ALL in False
  • Equal CORS_ALLOWED_ORIGINS to an array with the allowed domains

Also, I recommend use environment variables for ALLOWED_HOST, database credentials.

3. Inside the backend folder create a file named Procfile and copy this.

web: gunicorn --bind :8000 --workers 3 --threads 2 backend.wsgi:application

4. Create a folder named .ebextensions. then create a file called 01_package.config, this file will install yum and postgresSQL packages.

also create another file 02_python.config.

Configure AWS EB-CLI

Get AWS Security Key

The access key is used for commanding your AWS account from the console with eb-cli. In order to get this key login into your AWS Account then you have two options:

1.Go to My Security Credentials, then in Access key. Create a new key and download it. This key controls your account be cautious with it

2. Go to IAM services and create a new User group, this group must have permission for the services that you’re going to use like an elastic beanstalk (full access) and S3 console (full access). Then create a new user and associate it with our new group. After this, you can get an access key from this user.

Init Eb-cli — Create and Application

Open a console in the root of the Django project in the example “backend” folder. Then verify awsebcli version and initialize it.

eb --version
eb init
  • Select a default region: Select a region for your application
  • aws-access-id
  • aws-secret-key
  • Enter Application Name
  • Enter Application Name: Select an option of Linux 2
  • Do you want to set up SSH for your instances? Y
  • Select a keypair: Select or create a key for the EC2 instances

Great a new application has been created, the next step is to create an environment.

eb create
  • Enter Environment Name
  • Enter DNS CNAME prefix
  • Select a load balancer type: application
  • Would you like to enable Spot Fleet requests for this environment? N

This could take some minutes, don't worry if the operation has some errors. You could watch errors on the logs register.

eb logs

Any problem with installing libraries you can review“/var/log/eb-engine.log”. If there any problem with the libraries please solve it and try again the deploy.

eb deploy

If you use environment variables the status maybe could be severe but it can be solved later. By otherwise, if don't use environment variables check that ALLOWED_HOST and DATABASE are setting correctly. You can find the new allowed host in “/var/log/web.stdout.log”.

Connect an RDS PostgresSQL database

In this tutorial, I will connect an external database with Elastic Beanstalk. So before we have to verify that our RDS database has PostgreSQL same version that we installed. By another hand check that connectivity Security Group has the same that the Security Group for ElasticBeanstalk environment.

Also, check that the Security Group has the database port open. Greate Now we can connect with the database the hostname and port can find in RDS service. Then with PGAdmin, you can connect with the RDS database and create the database for your Django project.

Now in EC2 Services go to the Security Group for Elastic Beanstalk environment and check that the port used by PostgresSQL is open.

Environment Variables

Before continuing the deployment, you have to set in the environment variables in Elastic beanstalk Configurations. Once you apply the changes and if the variables are correct, your environment status must be OK. Otherwise, check logs.

Make migrations and create a superuser

Inside the Django project create a new app, then create a management folder inside it with an init file. Then create a command folder with two files init and createsu.py. Check the directory.

|_ apps
|__ app
|___ management
|____ commmands
|_____ createsu
|_____ __inti.py__
|____ __init.py__
|___ __init.py__

In createsu file, write this:

Then go to .ebextensions folder and create 03_django.config. In this file, you can make the command to migrate, create a super user and collect static.

Note: Before the deployment activates the python environment in your local Django folder and execute makemigrations and collect static files.

python manage.py makemigrations
python manage.py collectstatic

Now you are ready to deploy

eb deploy

If the deployment is successful, the status will be green in good health status. Otherwise, you can download the full logs from Elastic Beanstalk and check “/var/log/cfn-init-cmd.log” to check what is the error in the commands.

Great the deployment is complete. You can test your app if your application use CORS, you could have some problems with it but it will be solved in the second part.

Note from author

That’s all folks, thank you for reading this article — I hope you found this article useful. If you, have any questions please add them in the comments section.

If you need some help for creating the next big thing, you can contact our team on our website or at info@jrtec.io

You can also follow 👍 me on my Linkedin account 😄

--

--