A real-time chat application built with Python (Streamlit, FastAPI) and deployed on Google Cloud Platform using Terraform.
The infrastructure is deployed on Google Cloud Platform and managed by Terraform. The following diagram illustrates the components and traffic flow:
graph TD
subgraph "User & Test Traffic"
User[<fa:fa-user> User] -->|HTTPS| LB(External HTTPS LB)
StressTest[<fa:fa-rocket> Locust Stress Test] -->|HTTP/S| LB
end
subgraph "Google Cloud Infrastructure"
LB --> NEG(Serverless NEG)
NEG --> CloudRun(Cloud Run Service)
subgraph CloudRun [Cloud Run Service]
direction LR
Streamlit[<fa:fa-window-maximize> Streamlit Frontend]
FastAPI[<fa:fa-server> FastAPI Backend]
end
Streamlit <--> CloudSQL(Cloud SQL DB)
FastAPI <--> CloudSQL
CloudRun -->|Reads DB Password| SecretManager(Secret Manager)
end
subgraph "CI/CD Pipeline"
direction LR
GitHub[<fa:fa-github> GitHub Repo] --Push to main--> CBTrigger(Cloud Build Trigger)
CBTrigger --> CloudBuild(Cloud Build Job)
CloudBuild --Builds & Pushes Image--> ArtifactRegistry(Artifact Registry)
CloudRun --Pulls New Image for Deployments--> ArtifactRegistry
end
- Application: Python, Streamlit (Frontend), FastAPI (Backend)
- Database: Google Cloud SQL (PostgreSQL)
- Deployment: Docker, Google Cloud Run
- Infrastructure: Terraform, Google Cloud Load Balancing, Secret Manager
- CI/CD: Google Cloud Build, Artifact Registry, GitHub
- Load Testing: Locust
The Terraform configuration is modularized for clarity and independent deployment of components.
.
├── Dockerfile
├── entrypoint.sh
├── requirements.txt
├── src/ # Python application source code
├── stress/ # Locust stress test files
└── terraform/
├── main.tf # Root module to orchestrate deployment
├── terraform.tfvars
├── variables.tf
├── infra/ # Networking, IAM, and Load Balancer
├── app/ # Cloud Run, Cloud SQL, and the build process
└── cicd/ # Cloud Build trigger for GitHub integration
This project uses a modular Terraform setup. The infrastructure is deployed in layers, with dependencies managed automatically.
- A Google Cloud Platform project with the required APIs enabled.
- Google Cloud SDK authenticated (
gcloud auth application-default login). - Terraform installed.
- A GitHub repository connected to Google Cloud Build.
-
Configure Terraform:
- Update
terraform/terraform.tfvarswith your GCPproject_id,github_owner, andgithub_repo_name.
- Update
-
Deploy Infrastructure: Navigate to the
terraformdirectory and run the standard Terraform commands.cd terraform terraform init terraform apply -auto-approveThis command will:
- Provision the base infrastructure (
inframodule). - Build the Docker image and push it to Artifact Registry (
appmodule). - Deploy the Cloud Run service and Cloud SQL database (
appmodule). - Create the Cloud Build trigger for CI/CD (
cicdmodule).
- Provision the base infrastructure (
The project includes a load test using Locust to simulate user traffic. The test is defined in stress/locustfile.py and simulates two types of users:
- Readers: Browse the Streamlit frontend, generating read load on the database.
- Writers: Post messages directly to the FastAPI backend, generating write load.
-
Install Locust:
pip install locust
-
Run the Test: Execute the following command from the project root, replacing the host with your application's URL.
locust -f stress/locustfile.py --host https://your-app-url.com --users 100 --spawn-rate 10 --run-time 5m --headless
This project is licensed under the terms of the Apache 2.0 License.
