2016525 - CC ASSIGNMENT 3 1 Launching TowFood Django App on AWS Elastic Beanstalk Sebastian Prentice University of Buckingham http://towfood2 - env.eba - hriwjts6.eu - west - 2.elasticbeanstalk.com/ 2016525 – CC ASSIGNMENT 3 2 Table of Contents Launching TowFood Django App on AWS El astic Beanstalk ................................ ............ 3 My application ................................ ................................ ................................ .................... 3 Deploying the Applicaion ................................ ................................ ............................... 3 Setting up your environment. ................................ ................................ ...................... 3 Project Requirements ................................ ................................ ................................ .. 3 Create .ebextensions/django.config ................................ ................................ ............ 4 Allowed hosts ................................ ................................ ................................ .............. 4 Zip project for upload ................................ ................................ ................................ 4 Create EC2 application on elastic beanstalk. ................................ .............................. 4 Static Files. ................................ ................................ ................................ .................. 4 Database Issues. ................................ ................................ ................................ .......... 5 Creating a database on AWS. ................................ ................................ ...................... 7 Handling database migrations and creating the superuser. ................................ ......... 7 AWS CLI ................................ ................................ ................................ ..................... 7 AWS Credentials ................................ ................................ ................................ ......... 8 eb ssh ................................ ................................ ................................ ........................... 8 My Experience. ................................ ................................ ................................ ............... 8 References ................................ ................................ ................................ .......................... 9 2016525 – CC ASSIGNMENT 3 3 Launching TowFood Django App on AWS Elastic Beanstalk For the Cloud Computing Module at the University of Buckingham, School of Computing, students were tasked with launching a service or app on AWS Beanstalk My a pplication The application I will be working with i n my University of Buckingham final project, An Inventory management system . The system is build using Django web framew ork with Tailwind css, class - based styling. The Inventory system has three main functions. Record ingoing stock, Record outgoing stock and generate reports on details such as, suppliers, members, volunteers and total stock during a specific date range. I c hose this application as it was an initial goal of mine to push my final project to deployment. Deploying the a pplicaion To deploy the application, I am choosing to use AWS Elastic Beanstalk, and its free tier to create an environment suitable to host the application on the cloud. Setting up your environment In order to run an application or service on the cloud you must first be able to run the app/service in a local environment. In my case, as my project is written in python. I first need to create a pyt hon environment inside of my project directory: python - m venv env Project Requirements Once the environment has been made, ensure all project modules required have been installed inside of the environment , then run. pip freeze > requirements.txt to save all the project dependencies. 2016525 – CC ASSIGNMENT 3 4 Create .ebextensions/django.config Inorder to run the application on EC2 elastic beanstalk a . ebextensions folder needs to be created inside the project directory. Inside the .ebextensions folder create a file named django.config, Inside the django.config : option_settings: aws:elasticbeanstalk:container:python: WSGIPath: {{Your app name}} .wsgi:applicatio n Allowed hosts Inorder for the application to recognise the EC2 container when deployed edit the settings.py file and change/add: ALLOWED_HOSTS =[‘*’] Zip project for upload . Zip your project directory in preparation to create the EC2 on elastic beanstalk. On mac use this command in the project directory to zip the project and all the essential hidden files. zip ../myapp.zip - r * .[^.]* Create EC2 application on elastic bean stalk Navigate to Elastic beanstalk on the AWS console, and create application, select python 3.8, and upload the zip. Once the file has uploaded the project should take a few minutes to deploy. Upon deployement the page should now be live. Static Files If your site now does not have any styling or static files, first ensure your django project has the latest static files: python manage.py collectstatic then edit the django.config : aws:elasticbeanstalk:environment:proxy:staticfiles: /static: /sta tic 2016525 – CC ASSIGNMENT 3 5 This will tell the EC2 where your static files are located. You can check that the static files have been navigated to correctly by navigating to you environement on the AWS console, selecting Configuration from the sidebar then and Database Issues Now that your site is deployed with styling you might come across the issue related to the version of SQLite : deterministic = True requires SQLite 3.8.3 or higher There are currently 2 possible solutions to this problem: 1: use a different database engine 2: Downgrade the environment to Python 3.7 For me I tried option 2 first as I already has a database filled with sample data. But upon creating a new elastic beanstalk environment on python 3.7 I dicovered a lot of the packages I was using required python 3.8 or newer. Im sure I could have persued this option by downgrading my packages, however with further research it appears SQLite is almost never used in actual deployment, thus I went with option 1. Switching to Postgresql. Assuming you a lready have postgres downloaded locally, open pg_admin and create a database. Once the database is created navigate to settings.py and change the DATABASES = { sql...etc} to: if 'RDS_DB_NAME' in os.environ: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': os.environ['RDS_DB_NAME'], 2016525 – CC ASSIGNMENT 3 6 'USER': os.environ['RDS_USERNAME'], 'PASSWORD': os.environ['RDS_PASSWORD'], 'HOST': os.e nviron['RDS_HOSTNAME'], 'PORT': os.environ['RDS_PORT'], } } else: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': ' your _db', 'USER': 'postgres', 'PASSWORD': ' your_password , 'HOST': 'localhost', 'PORT': '5432', } } Then you need to run: python manage.py makemigrations if no changes appear be sure to delete the migrations folder and try again. After the migrations have been made run: python manage.py migrate. This will create all your tables in your postgres local database. 2016525 – CC ASSIGNMENT 3 7 Creating a database on AWS. First navigate to the elastic beanstalk environment page in your browser. Then select configuration in the sidebar. Scroll to the bottom under “Data Tier” click the link labelled “create a new RDS database”. On the RDS setup page change the DB engine to “postgres” add a master username and master password and save the changes. Handling data base migrations and creating the superuser. There are two methods to handling the database migrations and creating the super user. The first method requires using container_commands in your django.config to first start your python environent and run the co mmands: python manage.py makemigrations pyhton manage.py migrate python manage.py createsuperuser I tried this method but would continue to have issues with running the commands, also I found it very hard to figure out the path to the python environment my EC2 application was using. Thus I recommend you this second method. AWS CLI First install awsebcli: pip install awsebcli check to see if it installed correctly with: eb – version this should return the version of the package, and show that it has been installed correctly. Next we need to initialise our app with eb: eb init 2016525 – CC ASSIGNMENT 3 8 the initialisation process contains of naming you application, selecting the default region , I would recommend selecting the closest to you, and credentials AWS Credentials He ad over to IAM in the AWS console, then create a user and add the permissions “AdministratorAccess”. Once the user has been created navigate to the security credentials page on the user summary. Now navigate back to the terminal and generate an RSA keypair , the keypair will generate a SSH public key, copy and paste the public key from terminal, select the “upload SSH public key” page on the Users, security credentials page. Now you should be able to ssh to your application EC2 from your terminal. eb ssh On ce you are connected , u se a combination of ls: to list all the files un the current directiory, and cd {{ filepath }}: to change directory. Your aim is to find the location of your project directory. I found mine in /var/app/current Once you are in your app you can run source env/bin/activate to start your environment. Now that your environment is started you can run the commands: python manage.py makemigrations pyhton manage.py migrate python manage.py createsuperuser Your project and all its fu nctions should now work! My Experience. My experience deploying my project to AWS was awful, I probably spent 24+ hours on it, as there is very limited documentation. And the best resource was StackOverflow and a few articles below. The AWS documentation is very vague in most parts, making the d eveloper experience of the AWS console being pretty poor. 2016525 – CC ASSIGNMENT 3 9 References Create an Application Source Bundle - AWS Elastic Beanstalk https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/applications - sourcebundle.html. WonilWonil 6, et al. “Run Manage.py from AWS EB Linux Instance.” Stack Overflow , 1 Feb. 1961, https://stackoverflow.com/questions/19997343/run - manage - py - from - aws - eb - linux - instance. Deploying Django to the AWS Elastic Beanstalk Console. https://ordinarycoders.com/. Retrieved Nov 12, 2022, from https://ordinarycoders.com/blog/article/deploying - from - aws - eb - console How to Start Django Project with a Database(PostgreSQL). (2022). Medium. Retrieved Nov 12, 2022, from https://stackpython.medium.com/how - to - start - django - pro ject - with - a - database - postgresql - aaa1d74659d8 Python, R.Deploying Django + Python 3 + PostgreSQL to AWS Elastic Beanstalk – Real Python. Retrieved Nov 12, 2022, from https://realpython.com/deploying - a - django - app - and - postgresql - to - aws - elastic - beanstalk/