Skip to main content

Overview

Guardian API is deployed to Google Cloud Run, a fully managed serverless platform that automatically scales your containerized application. This guide covers deployment options and configuration.

Quick Deploy

The fastest way to deploy Guardian API to production:
1

Prerequisites

  • Google Cloud Platform account with billing enabled
  • gcloud CLI installed and configured
  • GitHub repository with backend code
2

Enable APIs

3

Create Artifact Registry

4

Deploy with Cloud Build

Your API will be deployed and accessible at a Cloud Run URL.

Deployment Options

Cloud Run Configuration

Resource Allocation

Production-ready configuration:

Update Configuration

Change resource allocation after deployment:

Environment Variables

Purpose: Specify which frontend domains can access your APIFormat: Comma-separated list of URLsExample:
Default:
  • https://guardian.korymsmith.dev
  • http://localhost:5173
  • http://127.0.0.1:5173
Set in Cloud Run:
Purpose: Control logging verbosityOptions: DEBUG, INFO, WARNING, ERROR, CRITICALDefault: INFOExample:
Purpose: Enable rate limiting with RedisFormat: rediss://default:<token>@<host>:<port>Example (Upstash Redis):
Note: Rate limiting fails open if not configured (allows all requests)Set securely:
Purpose: Improve HuggingFace model download reliabilityNote: Public models don’t require a token, but having one can help with rate limitsGet token: HuggingFace SettingsSet securely:

Continuous Deployment

Set up automatic deployments when you push to GitHub:
1

Connect Repository

2

Configure Trigger Settings

In the Google Cloud Console:
  • Navigate to Cloud Build > Triggers
  • Find your trigger
  • Add substitution variables:
    • _CORS_ORIGINS: Your frontend URL
    • _LOG_LEVEL: INFO (or DEBUG)
3

Automatic Deployments

Every push to main now:
  1. Triggers Cloud Build
  2. Builds Docker image
  3. Pushes to Artifact Registry
  4. Deploys to Cloud Run
  5. Creates new revision

Health Monitoring

Health Endpoint

Check API health and model status:
Expected Response:

View Logs

Monitor your API in real-time:

Cloud Run Metrics

View in Google Cloud Console:
  • Request count and rate
  • Response latency (p50, p95, p99)
  • Error rate
  • Instance count
  • Memory and CPU usage

Troubleshooting

Check logs:
Common issues:
  • Missing dependencies in requirements.txt
  • Python version mismatch (requires 3.11+)
  • Model files not included in Docker build
  • Insufficient memory allocation (needs 2Gi minimum)
Symptoms:
  • Health endpoint shows models as “not loaded”
  • Moderation requests fail with 500 errors
Solutions:
  1. Check logs for HuggingFace download errors
  2. Verify sufficient memory (2Gi recommended)
  3. Add HUGGINGFACE_HUB_TOKEN if rate limited
  4. Increase timeout to 300s for model loading
Symptoms:
  • Frontend can’t connect to API
  • Browser console shows CORS errors
Solutions:
  1. Verify CORS_ORIGINS includes your frontend URL
  2. Check for exact match (including https://)
  3. Avoid trailing slashes in URLs
  4. Verify environment variable is set:
Cause: Cloud Run scales to zero when idleBehavior: First request after idle takes 10-30 secondsSolutions:
  • Accept delay (most cost-effective)
  • Set minimum instances:
    Note: This prevents scale-to-zero (increases cost)
  • Use Cloud Scheduler to ping API every 5 minutes

Cost Optimization

Free Tier

Google Cloud Run includes generous free tier:
  • 2 million requests/month
  • 360,000 GB-seconds of memory
  • 180,000 vCPU-seconds
Most small to medium applications stay within free tier.

Cost Management

Scale to Zero

Set min-instances=0 to avoid charges when idleSavings: Only pay for actual usageTrade-off: Cold start delays

Right-Size Resources

Start with 2Gi memory and scale if neededMonitor: Check Cloud Run metrics for actual usageAdjust: Increase only if hitting limits

Set Billing Alerts

Configure budget alerts in GCPRecommended: Alert at 50%, 80%, 100% of budgetPrevents: Unexpected charges

Limit Max Instances

Cap auto-scaling to prevent runaway costsRecommended: Start with 10 max instancesAdjust: Based on traffic patterns

Security Best Practices

1

Use Secret Manager

Never commit secrets to repository:
2

Configure CORS Properly

Only allow specific frontend origins:
3

Enable Rate Limiting

Configure Redis to prevent abuse:
See Rate Limiting for details.
4

Monitor Logs

Regularly check logs for suspicious activity:

Next Steps

Configure Environment

Set up environment variables and configuration

Enable Rate Limiting

Protect your API with rate limiting

API Reference

Explore API endpoints and schemas

Architecture Guide

Understand the system architecture

Additional Resources

  • Detailed Deployment Guide: backend/DEPLOYMENT.md in repository
  • Cloud Build Config: backend/cloudbuild.yaml
  • Google Cloud Run Docs: cloud.google.com/run
  • Docker Configuration: backend/Dockerfile

Production Checklist

Before going live, verify: