GCP Deployment Guide for Phishing Detection API
This guide explains how to deploy the Phishing Detection API to Google Cloud Platform (GCP) using Docker and Cloud Run.
Prerequisites
- Google Cloud Platform Account: Ensure you have a GCP account and billing enabled
- gcloud CLI: Install the Google Cloud SDK
- Docker: Install Docker Desktop or Docker Engine
- GROQ API Key: Obtain your API key from Groq
Quick Start
Option 1: Using Cloud Build (Recommended)
Set your GCP project:
gcloud config set project YOUR_PROJECT_IDRun the deployment script:
# For Linux/Mac chmod +x deploy.sh ./deploy.sh YOUR_PROJECT_ID us-central1 # For Windows PowerShell .\deploy.ps1 -ProjectId YOUR_PROJECT_ID -Region us-central1The script will:
- Enable required APIs
- Create the GROQ_API_KEY secret in Secret Manager
- Build the Docker image
- Deploy to Cloud Run
Option 2: Manual Deployment
Step 1: Set Up GCP Project
# Set your project
gcloud config set project YOUR_PROJECT_ID
# Enable required APIs
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable containerregistry.googleapis.com
gcloud services enable secretmanager.googleapis.com
Step 2: Create Secret for GROQ API Key
# Create the secret
echo -n "your-groq-api-key" | gcloud secrets create GROQ_API_KEY \
--data-file=- \
--replication-policy="automatic"
# Grant Cloud Run access to the secret
PROJECT_NUMBER=$(gcloud projects describe YOUR_PROJECT_ID --format="value(projectNumber)")
gcloud secrets add-iam-policy-binding GROQ_API_KEY \
--member="serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com" \
--role="roles/secretmanager.secretAccessor"
Step 3: Build and Deploy
# Using Cloud Build (recommended)
gcloud builds submit --config=cloudbuild.yaml
# Or build locally and push
docker build -t gcr.io/YOUR_PROJECT_ID/phishing-detection-api:latest .
docker push gcr.io/YOUR_PROJECT_ID/phishing-detection-api:latest
# Deploy to Cloud Run
gcloud run deploy phishing-detection-api \
--image gcr.io/YOUR_PROJECT_ID/phishing-detection-api:latest \
--region us-central1 \
--platform managed \
--allow-unauthenticated \
--memory 4Gi \
--cpu 2 \
--timeout 300 \
--max-instances 10 \
--set-env-vars PYTHONUNBUFFERED=1 \
--set-secrets GROQ_API_KEY=GROQ_API_KEY:latest
Configuration Options
Cloud Run Settings
The deployment uses these default settings (configured in cloudbuild.yaml):
- Memory: 4GB (required for ML models)
- CPU: 2 vCPUs
- Timeout: 300 seconds (5 minutes)
- Max Instances: 10
- Region: us-central1 (change in cloudbuild.yaml or deploy command)
Adjusting Resources
If you need more resources, modify cloudbuild.yaml:
- '--memory'
- '8Gi' # Increase memory for larger models
- '--cpu'
- '4' # Increase CPU for faster inference
Or deploy with custom settings:
gcloud run deploy phishing-detection-api \
--image gcr.io/YOUR_PROJECT_ID/phishing-detection-api:latest \
--memory 8Gi \
--cpu 4 \
--timeout 600 \
--max-instances 20
Verifying Deployment
Check service status:
gcloud run services describe phishing-detection-api --region us-central1Get service URL:
SERVICE_URL=$(gcloud run services describe phishing-detection-api \ --region us-central1 \ --format="value(status.url)") echo $SERVICE_URLTest health endpoint:
curl $SERVICE_URL/healthView logs:
gcloud run services logs read phishing-detection-api --region us-central1
API Endpoints
Once deployed, your service will have these endpoints:
- Root:
GET /- API information - Health Check:
GET /health- Service health and model status - Prediction:
POST /predict- Main prediction endpoint - API Docs:
GET /docs- Interactive API documentation (Swagger UI)
Testing the API
Using curl:
curl -X POST "$SERVICE_URL/predict" \
-H "Content-Type: application/json" \
-d '{
"sender": "test@example.com",
"subject": "Urgent Action Required",
"text": "Please verify your account at http://suspicious-site.com",
"metadata": {}
}'
Using Python:
import requests
url = "YOUR_SERVICE_URL/predict"
payload = {
"sender": "test@example.com",
"subject": "Urgent Action Required",
"text": "Please verify your account at http://suspicious-site.com",
"metadata": {}
}
response = requests.post(url, json=payload)
print(response.json())
Monitoring and Logging
View Logs in Console
- Go to Cloud Run Console
- Click on your service:
phishing-detection-api - Navigate to the "Logs" tab
View Logs via CLI
gcloud run services logs read phishing-detection-api --region us-central1 --limit 50
Set Up Alerts
- Go to Cloud Monitoring
- Create alerts for:
- Error rate
- Request latency
- Memory usage
- CPU utilization
Troubleshooting
Container fails to start
- Check logs:
gcloud run services logs read phishing-detection-api --region us-central1 - Verify models are present in the container
- Check memory limits (may need to increase)
Models not loading
- Ensure all model files are included in the Docker image
- Check model paths in
config.py - Verify model files exist in
models/,finetuned_bert/, andMessage_model/final_semantic_model/
GROQ API errors
- Verify the secret is correctly set:
gcloud secrets versions access latest --secret="GROQ_API_KEY" - Check IAM permissions for the Cloud Run service account
- Verify the API key is valid
High memory usage
- Increase memory allocation in Cloud Run settings
- Consider using model quantization
- Check for memory leaks in the application
Cost Optimization
Set minimum instances to 0: Scales to zero when not in use
gcloud run services update phishing-detection-api \ --min-instances 0 \ --max-instances 10Use appropriate instance sizes: Start with smaller instances and scale up if needed
Enable request concurrency: Reduce number of instances needed
gcloud run services update phishing-detection-api \ --concurrency 10
Security Considerations
Authentication: Currently deployed as public. Consider adding authentication:
gcloud run services update phishing-detection-api \ --no-allow-unauthenticatedAPI Keys: Store sensitive keys in Secret Manager (already configured)
VPC: Consider deploying in a VPC for additional network isolation
Updating the Service
To update after code changes:
# Rebuild and deploy
gcloud builds submit --config=cloudbuild.yaml
# Or manually
docker build -t gcr.io/YOUR_PROJECT_ID/phishing-detection-api:latest .
docker push gcr.io/YOUR_PROJECT_ID/phishing-detection-api:latest
gcloud run deploy phishing-detection-api \
--image gcr.io/YOUR_PROJECT_ID/phishing-detection-api:latest \
--region us-central1
Local Testing with Docker
Before deploying to GCP, test locally:
# Build the image
docker build -t phishing-detection-api:local .
# Run with environment variable
docker run -p 8000:8000 \
-e GROQ_API_KEY=your-api-key \
phishing-detection-api:local
# Test
curl http://localhost:8000/health
Support
For issues or questions:
- Check the logs first
- Verify all prerequisites are met
- Ensure models are properly loaded
- Review the API documentation at
/docsendpoint