- Google Cloud CLI installed and authenticated
- Docker installed locally
- OpenAI API Key from OpenAI platform
Create or update your Cargo.toml:
[package]
name = "google-run"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = { version = "0.7", features = ["json"] }
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11", features = ["json"] }# Set your project ID
export PROJECT_ID="your-project-id"
gcloud config set project $PROJECT_ID
# Enable required APIs
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable artifactregistry.googleapis.com# Deploy directly from source code
gcloud run deploy ai-chat-app \
--source . \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars="OPENAI_API_KEY=your-openai-api-key-here" \
--memory=512Mi \
--cpu=1 \
--concurrency=100 \
--timeout=300 \
--max-instances=10# Create Artifact Registry repository
gcloud artifacts repositories create ai-chat-repo \
--repository-format=docker \
--location=us-central1
# Configure Docker authentication
gcloud auth configure-docker us-central1-docker.pkg.dev
# Build and tag image
docker build -t us-central1-docker.pkg.dev/$PROJECT_ID/ai-chat-repo/ai-chat-app:latest .
# Push to registry
docker push us-central1-docker.pkg.dev/$PROJECT_ID/ai-chat-repo/ai-chat-app:latest
# Deploy from registry
gcloud run deploy ai-chat-app \
--image us-central1-docker.pkg.dev/$PROJECT_ID/ai-chat-repo/ai-chat-app:latest \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars="OPENAI_API_KEY=your-openai-api-key-here" \
--memory=512Mi \
--cpu=1Instead of passing the API key directly, use Google Secret Manager:
# Create secret
echo -n "your-openai-api-key-here" | gcloud secrets create openai-api-key --data-file=-
# Deploy with secret
gcloud run deploy ai-chat-app \
--source . \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-secrets="OPENAI_API_KEY=openai-api-key:latest" \
--memory=512Mi \
--cpu=1# Map custom domain
gcloud run domain-mappings create \
--service ai-chat-app \
--domain your-domain.com \
--region us-central1Create a cloudbuild.yaml for CI/CD:
steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'us-central1-docker.pkg.dev/$PROJECT_ID/ai-chat-repo/ai-chat-app:$COMMIT_SHA', '.']
# Push the container image to Artifact Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'us-central1-docker.pkg.dev/$PROJECT_ID/ai-chat-repo/ai-chat-app:$COMMIT_SHA']
# Deploy container image to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args:
- 'run'
- 'deploy'
- 'ai-chat-app'
- '--image'
- 'us-central1-docker.pkg.dev/$PROJECT_ID/ai-chat-repo/ai-chat-app:$COMMIT_SHA'
- '--region'
- 'us-central1'
- '--platform'
- 'managed'
- '--allow-unauthenticated'
images:
- 'us-central1-docker.pkg.dev/$PROJECT_ID/ai-chat-repo/ai-chat-app:$COMMIT_SHA'Your application will automatically receive these environment variables:
PORT- Set by Cloud Run (usually 8080)OPENAI_API_KEY- Your OpenAI API keyGOOGLE_CLOUD_PROJECT- Your project IDK_SERVICE- Service nameK_REVISION- Revision name
# Set minimum instances to 0 for cost savings
gcloud run services update ai-chat-app \
--region us-central1 \
--min-instances=0 \
--max-instances=10# View logs
gcloud run services logs tail ai-chat-app --region=us-central1
# View service details
gcloud run services describe ai-chat-app --region=us-central1- Use Secret Manager for sensitive data
- Enable IAM authentication for production
- Set up VPC connector if accessing private resources
- Configure CORS properly for web clients
- Use HTTPS (enabled by default on Cloud Run)
Common issues and solutions:
- Build failures: Check Rust version and dependencies
- Port binding: Ensure your app binds to
0.0.0.0:$PORT - Memory limits: Increase if getting OOM errors
- Cold starts: Consider minimum instances > 0
- API limits: Implement rate limiting and error handling
# Get service URL
SERVICE_URL=$(gcloud run services describe ai-chat-app --region=us-central1 --format='value(status.url)')
# Test the endpoint
curl $SERVICE_URL
# Test chat API
curl -X POST $SERVICE_URL/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "Hello, AI!"}'Your AI chat application will be available at the provided Cloud Run URL!