{
  "schema": "https://ai-atoms.com/schemas/skill-v1.json",
  "type": "skill",
  "id": "skill/gcp-cloud-run",
  "version": "1.0.0",
  "name": "Gcp Cloud Run",
  "description": "Specialized skill for building production-ready serverless applications on GCP. Covers Cloud Run services (containerized), Cloud Run Functions (event-driven), cold start optimization, and event-dri...",
  "system_prompt_fragment": "# GCP Cloud Run\n\n## Patterns\n\n### Cloud Run Service Pattern\n\nContainerized web service on Cloud Run\n\n**When to use**: ['Web applications and APIs', 'Need any runtime or library', 'Complex services with multiple endpoints', 'Stateless containerized workloads']\n\n```javascript\n```dockerfile\n# Dockerfile - Multi-stage build for smaller image\nFROM node:20-slim AS builder\nWORKDIR /app\nCOPY package*.json ./\nRUN npm ci --only=production\n\nFROM node:20-slim\nWORKDIR /app\n\n# Copy only production dependencies\nCOPY --from=builder /app/node_modules ./node_modules\nCOPY src ./src\nCOPY package.json ./\n\n# Cloud Run uses PORT env variable\nENV PORT=8080\nEXPOSE 8080\n\n# Run as non-root user\nUSER node\n\nCMD [\"node\", \"src/index.js\"]\n```\n\n```javascript\n// src/index.js\nconst express = require('express');\nconst app = express();\n\napp.use(express.json());\n\n// Health check endpoint\napp.get('/health', (req, res) => {\n  res.status(200).send('OK');\n});\n\n// API routes\napp.get('/api/items/:id', async (req, res) => {\n  try {\n    const item = await getItem(req.params.id);\n    res.json(item);\n  } catch (error) {\n    console.error('Error:', error);\n    res.status(500).json({ error: 'Internal server error' });\n  }\n});\n\n// Graceful shutdown\nprocess.on('SIGTERM', () => {\n  console.log('SIGTERM received, shutting down gracefully');\n  server.close(() => {\n    console.log('Server closed');\n    process.exit(0);\n  });\n});\n\nconst PORT = process.env.PORT || 8080;\nconst server = app.listen(PORT, () => {\n  console.log(`Server listening on port ${PORT}`);\n});\n```\n\n```yaml\n# cloudbuild.yaml\nsteps:\n  # Build the container image\n  - name: 'gcr.io/cloud-builders/docker'\n    args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-service:$COMMIT_SHA', '.']\n\n  # Push the container image\n  - name: 'gcr.io/cloud-builders/docker'\n    args: ['push', 'gcr.io/$PROJECT_ID/my-service:$COMMIT_SHA']\n\n  # Deploy to Cloud Run\n  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'\n    entrypoint: gcloud\n    args:\n      - 'run'\n      - 'deploy'\n      - 'my-service'\n      - '--image=gcr.io/$PROJECT_ID/my-service:$COMMIT_SHA'\n      - '--region=us-central1'\n      - '--platform=managed'\n      - '--allow-unauthenticated'\n      - '--memory=512Mi'\n      - '--cpu=1'\n      - '--min-instances=1'\n      - '--max-instances=100'\n     \n```\n\n### Cloud Run Functions Pattern\n\nEvent-driven functions (formerly Cloud Functions)\n\n**When to use**: ['Simple event handlers', 'Pub/Sub message processing', 'Cloud Storage triggers', 'HTTP webhooks']\n\n```javascript\n```javascript\n// HTTP Function\n// index.js\nconst functions = require('@google-cloud/functions-framework');\n\nfunctions.http('helloHttp', (req, res) => {\n  const name = req.query.name || req.body.name || 'World';\n  res.send(`Hello, ${name}!`);\n});\n```\n\n```javascript\n// Pub/Sub Function\nconst functions = require('@google-cloud/functions-framework');\n\nfunctions.cloudEvent('processPubSub', (cloudEvent) => {\n  // Decode Pub/Sub message\n  const message = cloudEvent.data.message;\n  const data = message.data\n    ? JSON.parse(Buffer.from(message.data, 'base64').toString())\n    : {};\n\n  console.log('Received message:', data);\n\n  // Process message\n  processMessage(data);\n});\n```\n\n```javascript\n// Cloud Storage Function\nconst functions = require('@google-cloud/functions-framework');\n\nfunctions.cloudEvent('processStorageEvent', async (cloudEvent) => {\n  const file = cloudEvent.data;\n\n  console.log(`Event: ${cloudEvent.type}`);\n  console.log(`Bucket: ${file.bucket}`);\n  console.log(`File: ${file.name}`);\n\n  if (cloudEvent.type === 'google.cloud.storage.object.v1.finalized') {\n    await processUploadedFile(file.bucket, file.name);\n  }\n});\n```\n\n```bash\n# Deploy HTTP function\ngcloud functions deploy hello-http \\\n  --gen2 \\\n  --runtime nodejs20 \\\n  --trigger-http \\\n  --allow-unauthenticated \\\n  --region us-central1\n\n# Deploy Pub/Sub function\ngcloud functions deploy process-messages \\\n  --gen2 \\\n  --runtime nodejs20 \\\n  --trigger-topic my-topic \\\n  --region us-central1\n\n# Deploy Cloud Storage function\ngcloud functions deploy process-uploads \\\n  --gen2 \\\n  --runtime nodejs20 \\\n  --trigger-event-filters=\"type=google.cloud.storage.object.v1.finalized\" \\\n  --trigger-event-filters=\"bucket=my-bucket\" \\\n  --region us-central1\n```\n```\n\n### Cold Start Optimization Pattern\n\nMinimize cold start latency for Cloud Run\n\n**When to use**: ['Latency-sensitive applications', 'User-facing APIs', 'High-traffic services']\n\n```javascript\n## 1. Enable Startup CPU Boost\n\n```bash\ngcloud run deploy my-service \\\n  --cpu-boost \\\n  --region us-central1\n```\n\n## 2. Set Minimum Instances\n\n```bash\ngcloud run deploy my-service \\\n  --min-instances 1 \\\n  --region us-central1\n```\n\n## 3. Optimize Container Image\n\n```dockerfile\n# Use distroless for minimal image\nFROM node:20-slim AS builder\nWORKDIR /app\nCOPY package*.json ./\nRUN npm ci --only=production\n\nFROM gcr.io/distroless/nodejs20-debian12\nWORKDIR /app\nCOPY --from=builder /app/node_modules ./node_modules\nCOPY src ./src\nCMD [\"src/index.js\"]\n```\n\n## 4. Lazy Initialize Heavy Dependencies\n\n```javascript\n// Lazy load heavy libraries\nlet bigQueryClient = null;\n\nfunction getBigQueryClient() {\n  if (!bigQueryClient) {\n    const { BigQuery } = require('@google-cloud/bigquery');\n    bigQueryClient = new BigQuery();\n  }\n  return bigQueryClient;\n}\n\n// Only initialize when needed\napp.get('/api/analytics', async (req, res) => {\n  const client = getBigQueryClient();\n  const results = await client.query({...});\n  res.json(results);\n});\n```\n\n## 5. Increase Memory (More CPU)\n\n```bash\n# Higher memory = more CPU during startup\ngcloud run deploy my-service \\\n  --memory 1Gi \\\n  --cpu 2 \\\n  --region us-central1\n```\n```\n\n## Anti-Patterns\n\n### ❌ CPU-Intensive Work Without Concurrency=1\n\n**Why bad**: CPU is shared across concurrent requests. CPU-bound work\nwill starve other requests, causing timeouts.\n\n### ❌ Writing Large Files to /tmp\n\n**Why bad**: /tmp is an in-memory filesystem. Large files consume\nyour memory allocation and can cause OOM errors.\n\n### ❌ Long-Running Background Tasks\n\n**Why bad**: Cloud Run throttles CPU to near-zero when not handling\nrequests. Background tasks will be extremely slow or stall.\n\n## ⚠️ Sharp Edges\n\n| Issue | Severity | Solution |\n|-------|----------|----------|\n| Issue | high | ## Calculate memory including /tmp usage |\n| Issue | high | ## Set appropriate concurrency |\n| Issue | high | ## Enable CPU always allocated |\n| Issue | medium | ## Configure connection pool with keep-alive |\n| Issue | high | ## Enable startup CPU boost |\n| Issue | medium | ## Explicitly set execution environment |\n| Issue | medium | ## Set consistent timeouts |\n\n## When to Use\nThis skill is applicable to execute the workflow or actions described in the overview.",
  "applicable_domains": [
    "devops"
  ],
  "category": "devops",
  "invocation": [
    "/gcp-cloud-run"
  ],
  "authored_by": "claudeskills.in community",
  "source_url": "https://claudeskills.in/skill/gcp-cloud-run",
  "provenance": {
    "source": "claudeskills.in",
    "source_url": "https://claudeskills.in/skill/gcp-cloud-run",
    "license": "Apache-2.0",
    "imported_at": "2026-09-03",
    "notes": "Aggregated by claudeskills.in from community GitHub lists. Upstream as recorded by the aggregator: vibeship-spawner-skills (Apache 2.0)."
  },
  "tags": [
    "claudeskills",
    "devops"
  ],
  "lifecycle": "draft"
}