The article provides a comprehensive guide on deploying a Python visualization Panel App to Google Cloud Run using Google Cloud Build and Terraform for cost-effective and scalable hosting.
Abstract
The article by Sophia Yang, published on Dec. 24, 2021, outlines a method for deploying a Python-based visualization dashboard to Google Cloud Run, which offers a more cost-effective alternative to Google Cloud App Engine by running the app only when it receives requests. The author details the setup process for a Google Cloud account, the creation of a Panel App with necessary files (app.py, requirements.txt, and Dockerfile), and the deployment steps using gcloud commands. Additionally, the article explains how to automate the deployment process using Cloud Build and Terraform for infrastructure management, ensuring that the deployment decisions are captured in code for reproducibility and clarity. The author also provides references to video tutorials and acknowledges the support received from Jim Bednar.
Opinions
The author suggests that Google Cloud Run is a more cost-effective solution for hosting Python visualization apps compared to Google Cloud App Engine due to its pay-per-use billing model.
The use of Cloud Build and Terraform is recommended for managing complex applications and multiple deployments, as it allows for infrastructure as code practices, providing a clear overview of the infrastructure setup.
The article emphasizes the importance of automating the deployment process to streamline the management of applications and infrastructure, especially as the complexity of the application or the number of apps increases.
The author provides a subjective recommendation for an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), suggesting it offers similar performance and functionality.
Deploy a Python Visualization Panel App to Google Cloud Run
Google Cloud Run, Google Cloud Build, and Terraform
In my last blog post, I wrote about how to deploy a Python visualization Panel App to Google Cloud App Engine. App Engine works well, but it can be expensive because the app runs continuously whether anyone is using it or not. Luckily, there is a cheaper option called Cloud Run that only runs when receiving requests.
In this article, I will walk you through how to deploy a Python app or dashboard to Google Cloud Run with three simple scripts, and also how to automate your Google Cloud setup via Cloud Build and Terraform.
Here is what my very simple example app looks like. It is hosted here, where it will be live for 3 months after this article appears. For more examples and inspirations on Panel apps, check out awesome-panel.org, panel.holoviz.org, and my previous blog post on HoloViz tools.
1. Setup
Set up your Google Cloud account:
The Cloud Run documentation describes the following steps you need to take to enable running a project:
This is the Python file that creates the Panel App. To run this app locally, you can simply do conda install panel hvplotand run panel serve app.py.
requirements.txt
This file lists all the package dependencies of our Panel app.
Dockerfile
The Dockerfile installs Python and this project’s dependencies, then runs the command panel serve to run our Panel app.
3. Deploy App to Google Cloud Run
Deploy your app (choose the service folder):
gcloud run deploy
After gcloud works hard to set up your project for a few minutes, you should be able to see your dashboard live on the service URL that you see in the command line, which will be something like “service-xxx-uc.a.run.app”.
4. Deploy App to Google Cloud Run via Cloud Build and Terraform
This step is not necessary for our simple app. But when your application gets complicated and when you have a lot of apps to manage, it’s often a good practice to use Terraform. Terraform is an “open-source infrastructure as code software tool”.
In step 3 when we deploy the app via gcloud run deploy, we made a lot of decisions in the command line. For example, we set the region of the project, enabled some needed APIs, and we allowed unauthenticated invocations to service. Terraform allows us to capture all those decisions in code so that we have a place to see how the infrastructure is set up.
To use Terraform with Google Cloud Run, there are two parts:
Part 1 is to use the cloudbuild.yaml file to build a container image to execute your app ( i.e., the “build image” step) and push your container image to Artifact Registry (i.e., the “push image” step).
cloudbuild.yaml
Run the following commands:
You might need to authenticate gcloud auth application-default login
gcloud builds submit
to submit a build using Google Cloud Build. After this step, your should see your container image listed at https://console.cloud.google.com/gcr/images/your-project/global/docker given your-project is your project ID and docker is the service name we defined in the cloudbuild.yaml file.
Part 2 is to use terraform to create a Google Run Service from the container image we just created.
main.tf
This terraform configuration file creates a “project” variable for us to define the project on the command line, enables the cloud run service, specifies the cloud run service to run the container image we just created in part 2, sets the service public, and return a service URL.
Run the following commands:
if you are running it locally: conda install -c conda-forge terraform
Initialize Terraform: terraform init
Create an execution plan and give us a chance to review: terraform plan -var project=your-project
Execute the Terraform plan: terraform apply -var project=your-project
After a few minutes, you should see your app URL showing up in the command line: demo-xxx-uc.a.run.app.
And then if you’d like to remove the Cloud Run service, terraform destroy -var project=your-project or delete the entire project.
Overall, this article uses three simple scripts to deploy a visualization Panel app to Google Cloud Run and uses another two files to set up Google Cloud Build and Terraform.
For those who are interested in a video tutorial, here is the video version of this article: