From dada24a5e66ff72a7336ac2a34e7b86305922be4 Mon Sep 17 00:00:00 2001
From: Robert Wilkins III <1147229+genome21@users.noreply.github.com>
Date: Thu, 20 Apr 2023 04:43:26 +0000
Subject: [PATCH] add spot instance support and pricing
---
GCP_DEPLOYMENT.md | 31 +++++++--
scripts/deploy_langflow_gcp_spot.sh | 101 ++++++++++++++++++++++++++++
scripts/walkthroughtutorial.md | 7 +-
scripts/walkthroughtutorial_spot.md | 88 ++++++++++++++++++++++++
4 files changed, 221 insertions(+), 6 deletions(-)
create mode 100644 scripts/deploy_langflow_gcp_spot.sh
create mode 100644 scripts/walkthroughtutorial_spot.md
diff --git a/GCP_DEPLOYMENT.md b/GCP_DEPLOYMENT.md
index 01686e4ed..0491dabb0 100644
--- a/GCP_DEPLOYMENT.md
+++ b/GCP_DEPLOYMENT.md
@@ -1,7 +1,30 @@
-# Running Langflow from a new Google Cloud project
-This guide will help you set up a Langflow Dev VM in a Google Cloud Platform project using Google Cloud Shell.
-> When cloudshell opens, select **Trust repo**. Some gcloud commands do not run in ephemeral cloudshell.
+# Run Langflow from a New Google Cloud Project
+This guide will help you set up a Langflow development VM in a Google Cloud Platform project using Google Cloud Shell.
+
+> **Note**: When Cloud Shell opens, be sure to select **Trust repo**. Some `gcloud` commands might not run in an ephemeral Cloud Shell environment.
+
+
+## Standard VM
[](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=walkthroughtutorial.md)
-This script sets up a Debian-based VM with the Langflow package, Nginx, and the necessary configurations to run the Langflow Dev environment.
\ No newline at end of file
+This script sets up a Debian-based VM with the Langflow package, Nginx, and the necessary configurations to run the Langflow Dev environment.
+
+
+## Spot/Preemptible Instance
+
+[](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=walkthroughtutorial.md)
+
+When running as a [spot (preemptible) instance](https://cloud.google.com/compute/docs/instances/preemptible), the code and VM will behave the same way as in a regular instance, executing the startup script to configure the environment, install necessary dependencies, and run the Langflow application. However, **due to the nature of spot instances, the VM may be terminated at any time if Google Cloud needs to reclaim the resources**. This makes spot instances suitable for fault-tolerant, stateless, or interruptible workloads that can handle unexpected terminations and restarts.
+
+## Pricing
+
+
+| Component | Regular Cost (Hourly) | Regular Cost (Monthly) | Spot/Preemptible Cost (Hourly) | Spot/Preemptible Cost (Monthly) | Notes |
+| -------------- | --------------------- | ---------------------- | ------------------------------ | ------------------------------- | ----- |
+| 100 GB Disk | - | $10/month | - | $10/month | Disk cost remains the same for both regular and Spot/Preemptible VMs |
+| VM (n1-standard-4) | $0.15/hr | ~$108/month | ~$0.04/hr | ~$29/month | The VM cost can be significantly reduced using a Spot/Preemptible instance |
+| **Total** | **$0.15/hr** | **~$118/month** | **~$0.04/hr** | **~$39/month** | Total costs for running the VM and disk 24/7 for an entire month |
+
+> For a more accurate breakdown of costs, please use the [**GCP Pricing Calculator**](https://cloud.google.com/products/calculator)
+
diff --git a/scripts/deploy_langflow_gcp_spot.sh b/scripts/deploy_langflow_gcp_spot.sh
new file mode 100644
index 000000000..7ddc93b96
--- /dev/null
+++ b/scripts/deploy_langflow_gcp_spot.sh
@@ -0,0 +1,101 @@
+# Set the VM, image, and networking configuration
+VM_NAME="langflow-dev"
+IMAGE_FAMILY="debian-11"
+IMAGE_PROJECT="debian-cloud"
+BOOT_DISK_SIZE="100GB"
+ZONE="us-central1-a"
+REGION="us-central1"
+VPC_NAME="default"
+SUBNET_NAME="default"
+SUBNET_RANGE="10.128.0.0/20"
+NAT_GATEWAY_NAME="nat-gateway"
+CLOUD_ROUTER_NAME="nat-client"
+
+# Set the GCP project's compute region
+gcloud config set compute/region $REGION
+
+# Check if the VPC exists, and create it if not
+vpc_exists=$(gcloud compute networks list --filter="name=$VPC_NAME" --format="value(name)")
+if [[ -z "$vpc_exists" ]]; then
+ gcloud compute networks create $VPC_NAME --subnet-mode=custom
+fi
+
+# Check if the subnet exists, and create it if not
+subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)")
+if [[ -z "$subnet_exists" ]]; then
+ gcloud compute networks subnets create $SUBNET_NAME --network=$VPC_NAME --region=$REGION --range=$SUBNET_RANGE
+fi
+
+# Create a firewall rule to allow TCP port 8080 for all instances in the VPC
+firewall_8080_exists=$(gcloud compute firewall-rules list --filter="name=allow-tcp-8080" --format="value(name)")
+if [[ -z "$firewall_8080_exists" ]]; then
+ gcloud compute firewall-rules create allow-tcp-8080 --network $VPC_NAME --allow tcp:8080 --source-ranges 0.0.0.0/0 --direction INGRESS
+fi
+
+# Create a firewall rule to allow IAP traffic
+firewall_iap_exists=$(gcloud compute firewall-rules list --filter="name=allow-iap" --format="value(name)")
+if [[ -z "$firewall_iap_exists" ]]; then
+ gcloud compute firewall-rules create allow-iap --network $VPC_NAME --allow tcp:80,tcp:443 --source-ranges 35.235.240.0/20 --direction INGRESS
+fi
+
+# Create the Cloud Router and NAT Gateway
+cloud_router_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(name)")
+if [[ -z "$cloud_router_exists" ]]; then
+ gcloud compute routers create $CLOUD_ROUTER_NAME --network $VPC_NAME --region $REGION
+fi
+
+nat_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(nats.name)")
+if [[ -z "$nat_exists" ]]; then
+ gcloud compute routers nats create $NAT_GATEWAY_NAME --router $CLOUD_ROUTER_NAME --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --enable-logging --region $REGION
+fi
+
+# Define the startup script as a multiline Bash here-doc
+STARTUP_SCRIPT=$(cat <<'EOF'
+#!/bin/bash
+
+# Update and upgrade the system
+apt -y update
+apt -y upgrade
+
+# Install Python 3 pip, Langflow, and Nginx
+apt -y install python3-pip
+pip install langflow
+apt-get -y install nginx
+
+# Configure Nginx for Langflow
+touch /etc/nginx/sites-available/langflow-app
+echo "server {
+ listen 0.0.0.0:8080;
+
+ location / {
+ proxy_pass http://127.0.0.1:7860;
+ proxy_set_header Host "\$host";
+ proxy_set_header X-Real-IP "\$remote_addr";
+ proxy_set_header X-Forwarded-For "\$proxy_add_x_forwarded_for";
+ }
+}" >> /etc/nginx/sites-available/langflow-app
+ln -s /etc/nginx/sites-available/langflow-app /etc/nginx/sites-enabled/
+sudo nginx -t
+sudo systemctl restart nginx
+langflow
+EOF
+)
+
+# Create a temporary file to store the startup script
+tempfile=$(mktemp)
+echo "$STARTUP_SCRIPT" > $tempfile
+
+# Create the VM instance with the specified configuration and startup script
+gcloud compute instances create $VM_NAME \
+ --image-family $IMAGE_FAMILY \
+ --image-project $IMAGE_PROJECT \
+ --boot-disk-size $BOOT_DISK_SIZE \
+ --machine-type=n1-standard-4 \
+ --metadata-from-file startup-script=$tempfile \
+ --zone $ZONE \
+ --network $VPC_NAME \
+ --subnet $SUBNET_NAME \
+ -preemptible
+
+# Remove the temporary file after the VM is created
+rm $tempfile
diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md
index b53d3c6d2..0d619090f 100644
--- a/scripts/walkthroughtutorial.md
+++ b/scripts/walkthroughtutorial.md
@@ -39,7 +39,8 @@ The script will:
3. Create a Compute Engine VM instance with the specified configuration and startup script
4. Configure Nginx to serve Langflow on TCP port 8080
-> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished.
+
+> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished.
In the next step, you'll learn how to connect to the Langflow VM.
@@ -75,7 +76,9 @@ gcloud compute routers delete nat-client --region us-central1 --quiet
The following network settings and services are used during this walkthrough. If you plan to continue using the project after the walkthrough, you may keep these configurations in place.
However, if you decide to remove them after completing the walkthrough, you can use the following gcloud commands:
-> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings.
+
+
+> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings.
```
gcloud compute firewall-rules delete allow-tcp-8080 --quiet
diff --git a/scripts/walkthroughtutorial_spot.md b/scripts/walkthroughtutorial_spot.md
new file mode 100644
index 000000000..168a67046
--- /dev/null
+++ b/scripts/walkthroughtutorial_spot.md
@@ -0,0 +1,88 @@
+# Deploy Langflow on Google Cloud Platform
+
+**Duration**: 45 minutes
+**Author**: [Robert Wilkins III](https://www.linkedin.com/in/robertwilkinsiii)
+
+## Introduction
+
+In this tutorial, you will learn how to deploy Langflow on [Google Cloud Platform](https://cloud.google.com/) (GCP) using Google Cloud Shell.
+
+This tutorial assumes you have a GCP account and basic knowledge of Google Cloud Shell. If you're not familiar with Cloud Shell, you can review the [Cloud Shell documentation](https://cloud.google.com/shell/docs).
+
+## Set up your environment
+
+Before you start, make sure you have the following prerequisites:
+
+- A GCP account with the necessary permissions to create resources
+- A project on GCP where you want to deploy Langflow
+
+[**Select your GCP project**]
+
+
+
+In the next step, you'll configure the GCP environment and deploy Langflow.
+
+## Configure the GCP environment and deploy Langflow
+Run the deploy_langflow_gcp_spot.sh script to configure the GCP environment and deploy Langflow:
+
+```sh
+gcloud config set project
+bash ./deploy_langflow_gcp.sh
+```
+
+The script will:
+
+1. Check if the required resources (VPC, subnet, firewall rules, and Cloud Router) exist and create them if needed
+2. Create a startup script to install Python, Langflow, and Nginx
+3. Create a Compute Engine VM instance with the specified configuration and startup script
+4. Configure Nginx to serve Langflow on TCP port 8080
+
+> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished.
+
+In the next step, you'll learn how to connect to the Langflow VM.
+
+## Connect to the Langflow VM
+To connect to your new Langflow VM, follow these steps:
+
+1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page and click on the external IP for your VM. Make sure to use HTTP and set the port to 8080
+
**or**
+3. Run the following command to display the URL for your Langflow environment:
+```bash
+export LANGFLOW_IP=$(gcloud compute instances list --filter="NAME=langflow-dev" --format="value(EXTERNAL_IP)")
+
+echo http://$LANGFLOW_IP:8080
+```
+
+4. Click on the Langflow URL in cloudshell to be greeted by the Langflow Dev environment
+
+Congratulations! You have successfully deployed Langflow on Google Cloud Platform.
+
+
+
+## Cleanup
+If you want to remove the resources created during this tutorial, you can use the following commands:
+
+```sql
+gcloud compute instances delete langflow-dev --zone us-central1-a --quiet
+
+gcloud compute routers nats delete nat-gateway --router nat-client --region us-central1 --quiet
+
+gcloud compute routers delete nat-client --region us-central1 --quiet
+
+```
+The following network settings and services are used during this walkthrough. If you plan to continue using the project after the walkthrough, you may keep these configurations in place.
+
+However, if you decide to remove them after completing the walkthrough, you can use the following gcloud commands:
+> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings.
+
+```
+gcloud compute firewall-rules delete allow-tcp-8080 --quiet
+
+gcloud compute firewall-rules delete allow-iap --quiet
+
+gcloud compute networks subnets delete default --region us-central1 --quiet
+
+gcloud compute networks delete default --quiet
+```