Deploying Streamlit to Cloud Run

In this article, I will introduce how to deploy Streamlit to Cloud Run.

You can check the source code from the following repository:

https://github.com/ryuseikakujo/streamlit-cloud-run/tree/main

Clone the Source Code

Clone the source code with the following command:

bash
$ git clone https://github.com/ryuseikakujo/streamlit-cloud-run.git

Setup

Perform the setup with the following steps:

  1. Create the Terraform Backend
  2. Create infrastructure resources with Terraform
  3. Configure GitHub Actions Secrets

Create the Terraform Backend

To manage the Terraform state, create a bucket in GCS (Google Cloud Storage). Please create the bucket from the GCP console.

Create Infrastructure Resources with Terraform

First, modify the terraform/backend.tf file by changing the bucket:

terraform/backend.tf
  backend "gcs" {
-   bucket = "my-streamlit-tfstate"
+   bucket = "<Your bucket name>"
    prefix = ""
  }

Next, modify the terraform/variables.tf:

terraform/variables.tf
 variable "project" {
   type    = string
-  default = "my-gcp-proj"
+  default = "<Your GCP project name>"
 }

 variable "repo_org_name" {
   type    = string
-  default = "ryuseikakujo"
+  default = "Your github repository organization name"
 }

 variable "repo_name" {
   type    = string
-  default = "streamlit-cloud-run"
+  default = "Your github repository name"
 }

Then, apply Terraform with the following commands:

bash
$ cd terraform
$ terraform init
$ terraform apply

The following resources will be created:

  • Workload Identity Pool
  • Service account for Cloud Run
  • Service account for GitHub Actions

Configure GitHub Actions Secrets

Set the environment variables passed to Cloud Run during the deployment in .github/workflows/cd_streamlit.yml. Set the following variables in GitHub Secrets:

  • VAR1
  • VAR2

With this setup, when commits are made to the main branch, the Docker image will be pushed to GCR and Cloud Run will be deployed via GitHub Actions.

References

https://github.com/ryuseikakujo/streamlit-cloud-run/tree/main

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!