2023-08-04
Deploying Streamlit to Cloud Run
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:
Clone the Source Code
Clone the source code with the following command:
$ git clone https://github.com/ryuseikakujo/streamlit-cloud-run.git
Setup
Perform the setup with the following steps:
- Create the Terraform Backend
- Create infrastructure resources with Terraform
- 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
:
backend "gcs" {
- bucket = "my-streamlit-tfstate"
+ bucket = "<Your bucket name>"
prefix = ""
}
Next, modify the 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:
$ 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