How to deploy your Django app with PostgreSQL on AWS
Tags: django, qovery, postgres, aws
Published: 2021-09-17 07:19:48

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](https://aws.com) account and a Qovery account. To create a Qovery account, just go to [qovery.com](https://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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zupqmr42zmqprufha4sr.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/etnrjm7vkijf84uxt8xi.png)
 
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 <b>Admins</b> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68bklt4um3azfd2pdd3j.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ytjd6a34ol8jek4whaw.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2bhz3j2zzccwy2rxqu4.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qt2c6mxx6cbn0722mb4.png)
    
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ow4kvspyh16xs87envk5.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fe0uzp6ycpvyli30ryc.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqyq4x5xz03uj4aqdi14.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/anxqqd43owcxc8wtzs1p.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0jngr6nm2e068tzvk86.png)
    
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jp77y5yn10e8lo3fvyw.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48fehjh46n1w4arzrf5t.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihh8in0970veksouh9or.png)
   

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhvnnb5fl6tlfihbq0g1.png)
Then in the name field, input qovery_permissions, and now click on 'Create policy'
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz3vdsjq6b3cjij0u1ia.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xp8j05x2ujkjgrxbjgm.png)
  
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](https://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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3v6io2r9o4cem9b4pnc6.png)

Choose your plan
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5psjjmnf795fd0tkozql.png)

Then input a name for your organization, and click 'Create'  
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99rct55mhw5cwi5ygtpa.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rg32lg56b9lpmc8d3rs6.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qv1u74yezyjfq5h90pg4.png)
  

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrivsdtgfk20yg6u0uvc.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6shhitzpkdw9lmse3tyx.png)

The next thing to do is to input our AWS credentials into this cluster. To do this, first click on 'Edit'  
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sd1vd6qxclgorygu9p82.png)

Then head over to credentials and click on Add
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xdr4miv1ef4h2xnoa78.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/etty7gslr2jw6j586xaf.png)
 
Now, on your cluster, click on install. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuultcbzyd4ss5w3ci1c.png)
 
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"]`

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

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5jddudiv4tlj4zahnk9.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itwatj2xbavw35whxskt.png)
  
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bwyrj25w01uei7jmd5w.png)

You should see a confirmation message that says that your app has been created
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28y3nhykkl4k0bqdbkyt.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g7wyp74nm2knk4uns1y5.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0u9jt2gwoijcjcq624qc.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzbzfuo3fq08sf9boo8r.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8kafj6gchbohf8wz0yj1.png)

Now, choose a database name, and under Type, choose POSTGRESQL and version 11.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sj4qtrvzbvckultgtyh4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l78gbj3st02chcj68i6x.png)
 
This will deploy your posgresql database.

Once your database is deployed and running, the database page should look like this
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhjbcitcocnlaqwrf4f5.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxqfnn8a3j6vnz8rc7js.png)

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](https://www.youtube.com/watch?v=Qlwv7FzHADM)