How we used Google App Engine to deploy Django Application (with Postgres DB)at DUIT Technologies
Google App Engine is a PaaS solution from Google Cloud Hosting service. It allows you to host planet-scalable apps on the same infrastructure as Google itself. We are a lean startup team at DUIT Technologies, with limited resources and funds. We required a simple solution which could simplify our hosting needs. Essentially, we needed the following:
- No need/minimum need for DevOps.
- Simple learning curve.
- No overhead for managing load balancers, traffic spikes, redundancy etc.
- Detailed logging and monitoring, available on web/mobile. (no grep or ssh!)
- Economic and budget friendly.
Google App Engine fits all the above requirements for our Duit Backend functionality. GAE is a PaaS solution, instead of an IaaS. IaaS requires you to manually manage the server and manage load balancers, traffic spikes, deployments etc. A PaaS like GAE, on the other hand, can automatically manage these DevOps tasks for you. You can develop a simple application, and let Google manage the DevOps, leaving you to focus on the most important part of the equation — “Your product”.
It is almost as intuitive as Heroku, but far cheaper. Additionally, you can leverage $300 credits that Google provides for an entire year of usage. This is much better than the small micro tier that AWS provides.
At Duit Technologies, we are creating next-gen backend service to support our mobile platform. We are leveraging GAE’s immense horsepower. In this blog, I will give you a detailed step-by-step overview of how we got started with using GAE for hosting our Django Application.
I will give a brief overview of Duit, and what we are building.
Duit is an ecards platform, that allows you to create your ecard, exchange it with anyone, and manage all your received ecards. You can sort, search and filter your received cards by date, time and location. For example, you can do a query like, “Show me all the people, I met in San Fransisco in the Blockchain conference.” Or, People who could be my prospective clients.” etc. Moreover, you can share your card with anyone, even if they are not using the mobile app.
If you are here, chances are that you know about Django but if you don’t, let me give you a quick overview of it.
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
We faced some incomplete documentation on Google’s official site, so we have made this step by step guide that will make it drop-dead easy for you to deploy the Django app on GAE.
The official deployment instructions are given on Google Cloud website. I suggest you read this complete guide because they don't teach you to fix the mid-deployment errors, but we will.
So without any further delay, let’s begin…
We are going to deploy the Django app on Google App Engine standard environment. Let’s get into it in easy steps:
Step 1: Creating a project on the Google Cloud Console.
It is the most mandatory step in the Django deployment on Google Cloud App Engine. Projects enable you to manage all the Google Cloud Platform resources for your app, including deployment, access control, billing, and services.
- Open Google Cloud Platform Console.
- In the drop-down menu at the top, select Create a project.
- Click Show advanced options. Under App Engine location, select your preferred location.
- Give your project a name.
- Make a note of the project ID, which might be different from the project name. The project ID is used in commands and in configurations.
- Enable the billing for your project, if you haven’t already enabled it yet. Click here to enable it.
Step 2: Make sure your system is capable of deployment.
First of all, Install the Google Cloud SDK. To do this here are the options.
- If you are using Windows OS, click here.
- If you are using MacOS, click here.
- If you are using RED HAT/CENTOS, click here.
- If you are using DEBIAN/UBUNTU, click here.
- If you are using other LINUX, click here.
After this, we head towards enabling Cloud APIs that helps us in our Django deployment. Click here to navigate to the Google Cloud Platform Console that automatically enables the APIs required for the deployment. The most important API used in this task is Google Cloud SQL API.
Now make sure the structure of your application directory should be in the following manner:
We at DUIT, working on a huge project to make the communications and interactions between businesses easy. That’s why our project directory contains huge files of code and dreams too. But just like all the trees have a root from which they originate, we have a manage.py file from where our whole code originates. In the same directory, where your manage.py file exists, make a new file named app.yaml. Inside that file, paste the following code.
Next step is to download the Cloud SQL Proxy. Actually, at DUIT we make direct changes to our Database on GAE from our local systems via a Proxy. This proxy creates localhost like environment on our system by connecting the GAE services online. To learn more about Cloud SQL Proxy, you can navigate to the following link: click here
To install the Cloud SQL Proxy click here.
Now it’s time to make some changes on the GAE to support our deployment. First, create a Cloud SQL instance for PostgreSQL Second Generation Instance. Name the instance whatever you want. After that navigate to your project directory using the command line and type the following command:
gcloud sql instances describe [YOUR_INSTANCE_NAME]
Copy the connectionName displayed after executing the above command.
Just after that initialize and start your cloud SQL proxy using the following command:
./cloud_sql_proxy -instances=[YOUR_INSTANCE_CONNECTION_NAME]=tcp:5431 // For linux or MacOS
cloud_sql_proxy.exe -instances=[YOUR_INSTANCE_CONNECTION_NAME]=tcp:5431 // For Windows
Here the [YOUR_INSTANCE_CONNECTION_NAME] is your connectionName you have copied a minute ago.
Let’s move towards our settings.py file to configure our database settings. Paste the following code into your settings.py file.
That’s all for your pre-deployment configurations. Let’s begin with deployment by the last step of this article.
Step 3: Finalize your Deployment
To finalize your deployment, let’s set up our local environment. Run the following commands:
// For Linux/MacOS
virtualenv env /* Create a virtual environment */
source env/bin/activate /* Activate the virtual environment */
pip install -r requirements.txt /* Install all the requirements */
/* Reflect all the changes to make in the database. */
python manage.py makemigrations
/* apply all the changes that are reflected by the above command */
python manage.py migrate
/* Run the application on our local server */
python manage.py runserver
/* Collect all static files to a single folder */
python manage.py collectstatic
/* finally deploy the application on GAE */
gcloud app deploy
Whoopie your app is deployed… We are very happy to help you anytime. With love, from DUIT.