A demonstration Next.js application showcasing Google Cloud Run features including autoscaling, health checks, secret management, and environment configuration.
Live Demo: https://nextjs-cloud-run-demo-843666072335.us-central1.run.app
- Health Check Endpoint -
/api/healthfor Cloud Run health monitoring - System Information - Displays Cloud Run environment variables and system info
- CPU Stress Test - Demonstrates autoscaling capabilities
- Secret Manager Integration - Create and retrieve secrets securely
- AI Chatbot - OpenAI GPT-3.5 Turbo integration with secure API key management
- Responsive UI - Modern interface built with Tailwind CSS
- Production-Ready - Optimized Docker build with multi-stage process
- Node.js 18+ (for local development)
- Docker (for containerization)
- Google Cloud SDK (
gcloudCLI) - A Google Cloud Project with billing enabled
- Docker Buildx (for ARM Mac users)
Set your Google Cloud project ID:
export GCP_PROJECT_ID=your-project-idThis environment variable is used throughout the deployment process and scripts.
-
Install dependencies:
npm install
-
Run the development server:
npm run dev
-
Open http://localhost:3000 in your browser
-
Set your Google Cloud project ID as an environment variable:
export GCP_PROJECT_ID=your-project-id # Example: export GCP_PROJECT_ID=halogen-basis-461109-u8
-
Run the deployment script:
./deploy.sh
The deployment script will use the GCP_PROJECT_ID environment variable automatically.
-
Set your project ID (if not already set):
export GCP_PROJECT_ID=your-project-id -
Build the Docker image:
# For ARM-based Macs (Apple Silicon) - REQUIRED for Cloud Run docker buildx build --platform linux/amd64 -t gcr.io/${GCP_PROJECT_ID}/nextjs-cloud-run-demo . # For Intel/AMD systems docker build -t gcr.io/${GCP_PROJECT_ID}/nextjs-cloud-run-demo .
-
Push to Google Container Registry:
docker push gcr.io/${GCP_PROJECT_ID}/nextjs-cloud-run-demo -
Deploy to Cloud Run:
gcloud run deploy nextjs-cloud-run-demo \ --image gcr.io/${GCP_PROJECT_ID}/nextjs-cloud-run-demo \ --platform managed \ --region us-central1 \ --allow-unauthenticated \ --set-env-vars="GOOGLE_CLOUD_PROJECT=${GCP_PROJECT_ID}"
- Cloud Run automatically checks
/api/health - Monitor health status in the Cloud Console
- Click "Run Stress Test" multiple times
- Open multiple browser tabs and run tests simultaneously
- Watch instance count increase in Cloud Console
The app displays Cloud Run-specific environment variables:
K_SERVICE- Service nameK_REVISION- Revision nameK_CONFIGURATION- Configuration name
- Test concurrent requests to see load distribution
- Monitor request latency in Cloud Console metrics
- Create secrets using the web interface
- Retrieve secrets securely
- The deployment script automatically:
- Enables Secret Manager API
- Creates a demo secret
- Grants the Cloud Run service account access
# Create a secret manually
echo -n "my-secret-value" | gcloud secrets create test-secret --data-file=-
# Get your Cloud Run service account
SERVICE_ACCOUNT=$(gcloud run services describe nextjs-cloud-run-demo \
--region=us-central1 \
--format='value(spec.template.spec.serviceAccountName)')
# Grant access to Cloud Run service account
gcloud secrets add-iam-policy-binding test-secret \
--member="serviceAccount:${SERVICE_ACCOUNT}" \
--role="roles/secretmanager.secretAccessor"
# Use the web UI to retrieve the secretEdit deploy.sh or use gcloud commands:
--memory 256Mi|512Mi|1Gi|2Gi|4Gi|8Gi
--cpu 1|2|4Control requests per container:
--concurrency 80 # Default
--concurrency 1000 # High concurrencyConfigure min/max instances:
--min-instances 0 # Scale to zero
--max-instances 100 # Maximum instancesgcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=nextjs-cloud-run-demo" --limit 50Visit the Cloud Console to see:
- Request count and latency
- Instance count and CPU utilization
- Memory usage
- Error rates
- Scale to Zero: No charges when not in use
- Request-based Billing: Pay only for actual usage
- Efficient Containers: Optimized image size (~150MB)
- Ensure Docker daemon is running
- Check Node.js version compatibility
- Create a
publicdirectory if missing:mkdir -p public
- Verify gcloud authentication:
gcloud auth list - Check project permissions
- Ensure APIs are enabled
- Architecture Mismatch: If you see "Container manifest type must support amd64/linux", use
docker buildx build --platform linux/amd64 - Wrong Project: Ensure your image registry project matches your deployment project
Cloud Run requires AMD64/Linux images. Always build with:
docker buildx build --platform linux/amd64 -t gcr.io/PROJECT_ID/IMAGE_NAME .- Increase memory allocation
- Enable min-instances for warm starts
- Check region proximity to users
test-gcp/
├── app/ # Next.js app directory
│ ├── api/ # API routes
│ │ ├── chat/ # AI chatbot endpoint
│ │ ├── health/ # Health check endpoint
│ │ ├── info/ # System information
│ │ ├── secrets/ # Secret Manager demo
│ │ └── stress/ # CPU stress test
│ ├── page.tsx # Main dashboard
│ └── layout.tsx # Root layout
├── Dockerfile # Multi-stage build
├── deploy.sh # Deployment automation
└── package.json # Dependencies