DEV Community

Cover image for How to deploy your Django app with PostgreSQL on AWS
Tomi Tokko
Tomi Tokko

Posted on

How to deploy your Django app with PostgreSQL on AWS

If you want to deploy a Django project to AWS, then Qovery is the right platform for you. In this post, you will learn how to deploy your Django application to AWS using Qovery.

The first thing you need to do is to make sure that you have an AWS account and a Qovery account. To create a Qovery account, just go to qovery.com and signup with either your Github or Gitlab account. Once you have these two ready, you can move on with the tutorial.

To be able to use AWS with Qovery, you need to connect these two together. The first step to connecting your AWS account to Qovery is by creating your AWS credentials with the right IAM permissions.

Create AWS Credentials

We need to get two credentials from our AWS account, which are access key id and secret access key. To get these credentials, you need to go into your AWS console, and search for IAM. The first result that comes up is IAM, and it has a subtitle of Manage access to AWS resources
image

Click on IAM, and it should take you to the IAM (Identity and Access Management) dashboard. In there, navigate to the left corner of the page and click on User groups
image

In User groups, head over to the top-right corner of the page and click on Create group. Under user group name, input Admins (NOTE: You must input Admins as the name of the group. If another name is used instead, it will not work). Ignore where you see 'Add users to the group' and 'Attach permissions policies', then scroll down and click on Create group. You should get a green confirmation that the group has been successfully created, and you should also see the Admins group now listed in the list of groups. This step is visualized with the images below:
image
image
image
image

The next thing we need to do is to create a new user. To do this, head over to the left corner of the page and click on 'Users'
image

Now, head to the top right corner and click on 'Add users'. Then input qovery as the User name, and under 'Select AWS access type', tick the option that says 'Access key - Programmatic access', then click on Next.

In permissions, it will automatically select the option 'Add user to group', under that, tick the Admins group we created earlier. After that, click on Next twice, and then hit Create user.

Once you have these done, you should now have your Access key ID and Secret access key. Make sure you download this information as a csv file so that you don't loose it, because, when you go away from that page or close that tab, you will not be able to access the Secret access key again.

image
image
image
image

The next thing we need to do is to setup IAM permissions to the 'qovery' user we created earlier.

Under the list of users, click on qovery. When you click on it, it will take you to a page which contains the summary of that user. In there you will see a text that says 'Add inline policy', click on it, and it should take you to a page that says 'Create Policy'. In that page, switch over to the JSON section.
image
image
image

In the JSON tab, you need to replace the code in there with the code below:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iam:*",
"s3:*",
"cloudwatch:*",
"autoscaling:*",
"application-autoscaling:*",
"elasticloadbalancing:*",
"organizations:DescribeAccount",
"organizations:DescribeOrganization",
"organizations:DescribeOrganizationalUnit",
"organizations:DescribePolicy",
"organizations:ListChildren",
"organizations:ListParents",
"organizations:ListPoliciesForTarget",
"organizations:ListRoots",
"organizations:ListPolicies",
"organizations:ListTargetsForPolicy",
"dynamodb:*",
"ecr:*",
"ec2:*",
"elasticache:*",
"cloudtrail:LookupEvents",
"kms:DescribeKey",
"kms:ListAliases",
"dynamodb:*",
"tag:GetResources",
"rds:*",
"ecs:*",
"eks:*",
"logs:*",
"events:DescribeRule",
"events:DeleteRule",
"events:ListRuleNamesByTarget",
"events:ListTargetsByRule",
"events:PutRule",
"events:PutTargets",
"es:AddTags",
"es:RemoveTags",
"es:ListTags",
"es:DeleteElasticsearchDomain",
"es:DescribeElasticsearchDomain",
"es:CreateElasticsearchDomain",
"events:RemoveTargets",
"kms:*"
],
"Resource": "*"
}
]
}

Once you have that JSON code in there, click on 'Review Policy'
image
Then in the name field, input qovery_permissions, and now click on 'Create policy'
image
image

You have successfully created your AWS credentials. The next step is to connect it with Qovery

Connect AWS to Qovery

Qovery is a platform that helps in deploying web apps. Before following along it this tutorial, make sure that you are signed up to Qovery. To do that, heaad over to qovery.com.

The first thing to do here is to create a new organisation. Go to the top-right corner and click on New organization
image

Choose your plan
image

Then input a name for your organization, and click 'Create'

image

Now that your new organization is created, go into the settings of that organization by clicking the gear icon at the top-right corner of the organization page, and then click on 'Clusters' which is located in the left corner of the Settings page.
image
image

In the cluster section, click on 'Add Cluster'. Then input a name for the cluster, and under cloud provider, select 'Amazon Web Services' and also select a region the best suites you. Then click on Create. Your new cluster has now been created successfully.
image
image

The next thing to do is to input our AWS credentials into this cluster. To do this, first click on 'Edit'

image

Then head over to credentials and click on Add
image

Now, you need to enter a name for the new credential, and input your AWS Access Key ID and also your Secret Access Key, then click on save.
image

Now, on your cluster, click on install.
image

This will go ahead and install your cluster fully. This usually takes 30 minutes, but you will be notified when this process is done. Now, you have successfully installed Qovery on AWS!

The next thing to do now is to deploy our Django application.

Deploy A Django App

Now that we have everything setup, the next step is to deploy our Django app.

Before we start deploying, make sure that you have pushed your django project to the github account that you used to sign up to Qovery. Your django project must contain a 'requirements.txt' file and also two other files which are 'Dockerfile' and 'entrypoint.sh'. These files are used by Qovery to run and manage our application.

For the Dockerfile, just paste the code below into it:

FROM python:3.8.0-alpine

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev

RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

COPY djangofollow /usr/src/app/
COPY entrypoint.sh /usr/src/app

EXPOSE 8000

ENTRYPOINT ["./entrypoint.sh"]`
Enter fullscreen mode Exit fullscreen mode

In the 'entrypoint.sh' file, input this code in it:
#!/bin/sh
set -euo pipefail

python manage.py migrate

exec python manage.py runserver 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

Now that we have these done, we are good to go.

In the organization page, click on 'Create Project' and a new project will be created, now click on 'Create Environment' and input the name of the environment, and a new environment will be created.

image
image

Now, let's create a new application by clicking on the 'Add my first app' button. Then input the name of the app and then choose the Github repository of the app and also the Branch. Then click on 'Create'
image

You should see a confirmation message that says that your app has been created
image

Now, click on settings in the app and head over to the General section, in there, under Build mode, change the option from Buildpacks to Dockerfile, then click on save. Also go into port, and add a port of 8000, and click save again.

image

image

image

The next thing to do is to create a new postgresql database. To do this, head over to the environment, and at the top-right corner, click on Add, and under it click on Database
image

Now, choose a database name, and under Type, choose POSTGRESQL and version 11.
image

Once you click create, it will create a new postgresql database and it will redirect you to the database page. In there, got to the top-right corner and click on settings, under settings, click Deploy
image

This will deploy your posgresql database.

Once your database is deployed and running, the database page should look like this
image

Now that we have our app created and our postgresql database deployed, let's connect them together using environment and secret variables.

Go into the app, at the left part, head over to 'Environment Variables'. Now what you need to do is to got into the 'settings.py' file of your django project and set the database using this format:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('QOVERY_POSTGRESQL_Z5B5DF0B7_DEFAULT_DATABASE_NAME', 'postgres'),
'USER': os.environ.get('QOVERY_POSTGRESQL_Z5B5DF0B7_LOGIN', 'postgres'),
'PASSWORD': os.environ.get('QOVERY_POSTGRESQL_Z5B5DF0B7_PASSWORD'),
'HOST': os.environ.get('QOVERY_POSTGRESQL_Z5B5DF0B7_HOST', 'z5b5df0b7-postgresql.||Q_DOMAIN||'),
'PORT': os.environ.get('QOVERY_POSTGRESQL_Z5B5DF0B7_PORT', 5432),
}
}

All you need to do is to replace all the QOVERY_POSTGRESQL_XYXYXYX with the values in your Environment Variable.

Once you have done this, the final thing to do is to deploy your django app. Now, go into your app and click on Actions, under Actions, click Deploy
image

Congrats! You have successfully deployed your Django app with PostgreSQL On AWS using Qovery.

You can also watch the video version of this tutorial here

Top comments (2)

Collapse
 
rophilogene profile image
Romaric P.

wow thank you Tomi for such a nice tutorial (I think the most complete one) to deploy a Django app on AWS with Qovery. Thank you

Collapse
 
tomitokko3 profile image
Tomi Tokko

I'm glad you like it.