Deploying Django and Wagtail on Google Cloud

Tags: serverlessexpeditions, django, cloudrun
Published: 2020-10-01 23:03:15

On this edition of Serverless Expeditions, we take a look at how to deploy more complex applications on Google Cloud, using Django as an example. 

Check out the video version of this blog post.

{% youtube Pk21gaNO6ag %}

---

When moving legacy applications into serverless infrastructure, there is going to be some sort of state involved: there will be a database, media, and other components. Traditionally these can all be run within the confines of a virtual machine, where all the services can talk to each other in that environment. But when moving to serverless, all these components need to be specifically provisioned and connected together. 

While Cloud Run itself is stateless, it can be connected to other stateful services such as [Cloud SQL](https://cloud.google.com/sql/) for relational databases, or [Cloud Storage](https://cloud.google.com/storage) for static media hosting. 

In today's episode, we'll discuss [containerizing](https://www.youtube.com/watch?v=AoNulKfMl_Q&list=PLIivdWyY5sqKiWvnaA5A8F3UQ0Xu5i49U) and deploying a typical Django application using Cloud Run and other Google Cloud services. The steps described can be applied to any website whose CMS uses the [Django web framework](http://djangoproject.com/), such as [Wagtail](https://wagtail.io/) or [Django CMS](https://www.django-cms.org/en/). 



---


Django supports both PostgreSQL and MySQL, both of which are available in Cloud SQL, a managed database service. When connecting to a Cloud Run service, specifying <code>[--add-cloudsql-instances](https://cloud.google.com/sql/docs/postgres/connect-run#command-line)</code> allows the service and the database to communicate over an automatically encrypted connection. The authentication from the application to the database is achieved by specifying a connection URL, stored securely in [Secret Manager](https://cloud.google.com/secret-manager), loaded using the [Python API](https://pypi.org/project/google-cloud-secret-manager/). The database can then be populated by running <code>[./manage.py migrate](https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-migrate)</code>

Static and media assets can be migrated to use Cloud Storage, with help from the <code>[django-storages](https://pypi.org/project/django-storages/)</code> package. By creating a new storage bucket and specifying the name in the django settings.py, then running <code>[./manage.py collectstatic](https://docs.djangoproject.com/en/3.0/ref/contrib/staticfiles/#collectstatic)</code>, the images and assets are uploaded to the bucket. 

However, those two manage.py commands -- `migrate` and `collectstatic` -- are typically run within an interactive terminal. Cloud Run has no such terminal, so these commands need to be run elsewhere. One method is to run these commands as part of the continuous integration process with [Cloud Build](https://cloud.google.com/cloud-build). Adding a 'migrate' step using the [app-engine-exec-wrapper](https://github.com/GoogleCloudPlatform/ruby-docker/tree/master/app-engine-exec-wrapper) between the typical build and deploy steps allows migration to happen as part of deployment. 

If database migrations need to be manual, Cloud SQL proxy can be used to interact with a [Cloud SQL database locally](https://cloud.google.com/sql/docs/postgres/connect-admin-proxy). Details of how to set this up with Docker Compose are [available here.](https://github.com/GoogleCloudPlatform/django-demo-app-unicodex/blob/latest/docs/70-manual-deployments.md)

With a managed database, highly-available static assets, automated deployment pipelines and robust containerisation solutions, you can migrate your Django applications to Google Cloud with confidence.



---


If you want to get started with these concepts, check out the Django Codelabs: 



*   [Django on Cloud Run](https://codelabs.developers.google.com/codelabs/cloud-run-django/#0)
*   [Wagtail on Cloud Run](https://codelabs.developers.google.com/codelabs/cloud-run-djangocms/#0)
*   [Django CMS on Cloud Run](https://codelabs.developers.google.com/codelabs/cloud-run-djangocms/#0)

You can also find a sample implementation of these concepts at [GoogleCloudPlatform/django-demo-app-unicodex](https://github.com/GoogleCloudPlatform/django-demo-app-unicodex).



---



## About Serverless Expeditions

Serverless Expeditions is a fun and cheeky video series that looks at what serverless means and how to build serverless apps with Google Cloud. 

Follow these hosts on Twitter at [@glasnt](https://twitter.com/glasnt) and [@martinomander](https://twitter.com/martinomander).