Part 4 : Creating and Configuring RDS With the Django Application
If your Django application using any database then you need to configure a production database. In this part, we are going to explore how we can use AWS RDS as our production database with our Django application. So let’s login to your AWS console and select RDS from the services list of AWS.
It will take you to the RDS management console and your will see All your resources. Scroll down to the Create Database section and click on
create database
Create Database button will take you to the database creation form which has the following sections.
Choose a database creation method:
AWS provides two methods to create a database:
1. Standard create: we have to set all of the configuration options.
2. Easy Create option, which will use recommended best-practice configurations to create your database.
We are going to go with the Standard create option so select that.
Engine option:
AWS gives 6 main database engine with RDS. You can choose any engine you want to use, We are going to go with MySQL So select that. Also if you want to change the version then you can change it from the dropdown. In my case, I am not going to work with the recent version so I will leave it as default.
Templates:
AWS provides Three templates to create your RDS database.
1. Production: Use defaults for high availability and fast, consistent performance.
2. Dev/Test: This instance is intended for development use outside of a production environment.
3. Free Tier: Use free Tier to develop new applications, test existing applications, or gain hands-on experience with Amazon RDS.
We are going to use the Free Tier so select that and go ahead.
Settings:
In this section, you are going to set your database credentials. First set the database Identifier, it is the identifier for your database. In my case, I have given it as SMARTTUTS.
Then under credentials settings set the username and password for your database.
DB instance size:
Here you are going to set the instance Specification for your DB. But in our case, as we are using the Free Tier service we should leave it default.
Storage:
Here you are going to allocate storage to your DB instance. We are going to allocate storage of GIB, if you want more storage you can specify. There is also an option as Storage Autoscaling. Enabling this feature will allow the storage to increase once the specified threshold is exceeded.
Availability & Durability:
As we are using Free Tier we don’t have to change anything in this section so leave it.
Connectivity:
Here you are going to specify the networking environment for the DB instance. You can create your own security group and use them or you can leave it as default.
Here there is also a subsection Additional connectivity configuration. In this section, we are going to make Publicly accessible YES so that our instance can access publicly from our EC2 instance. Leave other options as default.
Additional Configuration:
Here you are going to set the database name for the RDS account. Give a database name and proceed with other default options.
Estimated monthly cost:
Here the estimated cost for this instance will appear. As we are using Free Tier we don’t have to pay anything for this instance.
Finally, Click on the create database button.
Wait till the database being created and after the creation of the database click on the database name from the database list. It will open that database and show you all the configuration.
From the Connectivity & security section copy the value of endpoint and keep it somewhere, we are going to use that in our Django application.
Integrating RDS with Django Application:
After the successful creation of the database, we have to integrate the dataset with our Django application. So open settings.py of your Django application and find the database section.
There paste all the credentials like below.
#Production database DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'DATABASE_NAME', 'USER': 'USERNAME', 'PASSWORD': 'PASSWORD', 'HOST': 'END_POINT', 'PORT': '3306', } }
Note: Update all the capitalized field with your credential values, which you have configured while creating the RDS.
You can either change the settings.py in AWS console or you can update it in your local system and get it to EC2 using git.
Migrating the database for our Django application:
After creating the database we have to migrate the application with the new database so that the tables will be created. If you have logged out from the remote then take the remote of your instance like last time and execute the following commands.
python3 manage.py makemigrations
This command may show you an error that you have not installed mysqlclient. Use the bellow command to install mysqlclient.
sudo apt-get install python3 python-dev python3-dev \
build-essential libssl-dev libffi-dev \
libxml2-dev libxslt1-dev zlib1g-dev \
python-pip
Again execute that command this time it will show you that no changes detected. Now execute another command to migrate.
python3 manage.py migrate
Creating Super User for Django Admin:
To use Django admin you have to create a superuser, let’s create a superuser by the following command:
python3 manage.py createsuperuser
Note: It will ask you for username, Email, and password; give all the details correctly.
After creating the superuser restart the server
sudo service nginx restart
sudo supervisorctl reload
Now open your URL and go to the /admin and login.