GCP Cloud Functions Deep Management
Comprehensive analysis of Cloud Functions across both 1st gen and 2nd gen deployments.
Phase 1: Discovery
#!/bin/bash
PROJECT="${GOOGLE_CLOUD_PROJECT}"
echo "=== Cloud Functions (2nd Gen) ==="
gcloud functions list --project="$PROJECT" --gen2 \
--format="table(name.basename(), state, runtime, serviceConfig.availableMemory, serviceConfig.timeoutSeconds, serviceConfig.maxInstanceCount, updateTime)" \
2>/dev/null | head -20
echo ""
echo "=== Cloud Functions (1st Gen) ==="
gcloud functions list --project="$PROJECT" \
--format="table(name.basename(), status, runtime, availableMemoryMb, timeout, entryPoint, httpsTrigger.url:label=TRIGGER)" \
2>/dev/null | head -20
echo ""
echo "=== Trigger Types ==="
gcloud functions list --project="$PROJECT" \
--format="json" | jq -r '.[] | "\(.name | split("/") | last)\t\(.eventTrigger.eventType // "HTTP")"' \
| column -t | head -20
echo ""
echo "=== VPC Connectors ==="
gcloud compute networks vpc-access connectors list --project="$PROJECT" \
--format="table(name, network, ipCidrRange, state, minInstances, maxInstances)" 2>/dev/null
echo ""
echo "=== Runtime Distribution ==="
gcloud functions list --project="$PROJECT" --format="value(runtime)" \
| sort | uniq -c | sort -rn
Phase 2: Analysis
#!/bin/bash
PROJECT="${GOOGLE_CLOUD_PROJECT}"
echo "=== Function Execution Metrics (7d) ==="
END=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
START=$(date -u -d "7 days ago" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u -v-7d +"%Y-%m-%dT%H:%M:%SZ")
gcloud monitoring time-series list \
--project="$PROJECT" \
--filter='metric.type="cloudfunctions.googleapis.com/function/execution_count"' \
--interval-start-time="$START" --interval-end-time="$END" \
--format="table(metric.labels.function_name, points[0].value.int64Value:label=EXECUTIONS, metric.labels.status)" \
2>/dev/null | head -30
echo ""
echo "=== Execution Times ==="
gcloud monitoring time-series list \
--project="$PROJECT" \
--filter='metric.type="cloudfunctions.googleapis.com/function/execution_times"' \
--interval-start-time="$START" --interval-end-time="$END" \
--format="table(metric.labels.function_name, points[0].value.distributionValue.mean:label=AVG_NS)" \
2>/dev/null | head -20
echo ""
echo "=== Active Instances ==="
gcloud monitoring time-series list \
--project="$PROJECT" \
--filter='metric.type="cloudfunctions.googleapis.com/function/active_instances"' \
--interval-start-time="$START" --interval-end-time="$END" \
--format="table(metric.labels.function_name, points[0].value.int64Value:label=MAX_INSTANCES)" \
2>/dev/null | head -20
echo ""
echo "=== Secret Bindings Audit ==="
for FUNC in $(gcloud functions list --project="$PROJECT" --format="value(name)"); do
SECRETS=$(gcloud functions describe "$FUNC" --project="$PROJECT" --format="json" \
| jq -r '.secretEnvironmentVariables[]?.secret // empty' 2>/dev/null)
[ -n "$SECRETS" ] && echo "$(basename $FUNC): $SECRETS"
done
echo ""
echo "=== Build Configuration ==="
for FUNC in $(gcloud functions list --project="$PROJECT" --format="value(name)"); do
gcloud functions describe "$FUNC" --project="$PROJECT" --format="json" \
| jq '{name: (.name | split("/") | last), source: .sourceArchiveUrl, buildWorkerPool: .buildWorkerPool, dockerRegistry: .dockerRegistry}' 2>/dev/null
done | head -30
Output Format
GCP CLOUD FUNCTIONS DEEP ANALYSIS
===================================
Function Gen Runtime Memory Timeout Max-Inst Executions Errors Avg-ms
──────────────────────────────────────────────────────────────────────────────────────────────
process-orders 2nd nodejs20 512MB 60s 10 45200 12 120
auth-validate 2nd python312 256MB 30s 100 128900 3 45
image-resize 1st nodejs18 1024MB 120s 5 8900 0 2400
Runtimes: nodejs20(3) python312(2) go122(1)
VPC Connectors: 1 active | Secret Bindings: 2 functions
Safety Rules
- Read-only: Only use
gcloud functions list, describe, and monitoring queries
- Never deploy or delete functions without explicit user confirmation
- Secrets: Never output secret values, only report which secrets are bound
- Quota: Cloud Monitoring API has 6000 read requests per minute per project
Anti-Hallucination Rules
- NEVER assume resource names — always discover via CLI/API in Phase 1 before referencing in Phase 2.
- NEVER fabricate metric names or dimensions — verify against the service documentation or
--help output.
- NEVER mix CLI commands between service versions — confirm which version/API you are targeting.
- ALWAYS use the discovery → verify → analyze chain — every resource referenced must have been discovered first.
- ALWAYS handle empty results gracefully — an empty response is valid data, not an error to retry.
Counter-Rationalizations
| Shortcut |
Counter |
Why |
| "I'll skip discovery and check known resources" |
Always run Phase 1 discovery first |
Resource names change, new resources appear — assumed names cause errors |
| "The user only asked for a quick check" |
Follow the full discovery → analysis flow |
Quick checks miss critical issues; structured analysis catches silent failures |
| "Default configuration is probably fine" |
Audit configuration explicitly |
Defaults often leave logging, security, and optimization features disabled |
| "Metrics aren't needed for this" |
Always check relevant metrics when available |
API/CLI responses show current state; metrics reveal trends and intermittent issues |
| "I don't have access to that" |
Try the command and report the actual error |
Assumed permission failures prevent useful investigation; actual errors are informative |
1---2name: managing-gcp-cloud-functions-deep3description: Use when working with Gcp Cloud Functions Deep — deep Google Cloud Functions analysis covering function inventory across generations (1st and 2nd gen), trigger configurations, execution metrics, memory and CPU allocation, VPC connector usage, secret bindings, and build configuration review. Provides insights on cold starts and scaling.4---56# GCP Cloud Functions Deep Management78Comprehensive analysis of Cloud Functions across both 1st gen and 2nd gen deployments.910## Phase 1: Discovery1112```bash13#!/bin/bash14PROJECT="${GOOGLE_CLOUD_PROJECT}"1516echo "=== Cloud Functions (2nd Gen) ==="17gcloud functions list --project="$PROJECT" --gen2 \18 --format="table(name.basename(), state, runtime, serviceConfig.availableMemory, serviceConfig.timeoutSeconds, serviceConfig.maxInstanceCount, updateTime)" \19 2>/dev/null | head -202021echo ""22echo "=== Cloud Functions (1st Gen) ==="23gcloud functions list --project="$PROJECT" \24 --format="table(name.basename(), status, runtime, availableMemoryMb, timeout, entryPoint, httpsTrigger.url:label=TRIGGER)" \25 2>/dev/null | head -202627echo ""28echo "=== Trigger Types ==="29gcloud functions list --project="$PROJECT" \30 --format="json" | jq -r '.[] | "\(.name | split("/") | last)\t\(.eventTrigger.eventType // "HTTP")"' \31 | column -t | head -203233echo ""34echo "=== VPC Connectors ==="35gcloud compute networks vpc-access connectors list --project="$PROJECT" \36 --format="table(name, network, ipCidrRange, state, minInstances, maxInstances)" 2>/dev/null3738echo ""39echo "=== Runtime Distribution ==="40gcloud functions list --project="$PROJECT" --format="value(runtime)" \41 | sort | uniq -c | sort -rn42```4344## Phase 2: Analysis4546```bash47#!/bin/bash48PROJECT="${GOOGLE_CLOUD_PROJECT}"4950echo "=== Function Execution Metrics (7d) ==="51END=$(date -u +"%Y-%m-%dT%H:%M:%SZ")52START=$(date -u -d "7 days ago" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u -v-7d +"%Y-%m-%dT%H:%M:%SZ")5354gcloud monitoring time-series list \55 --project="$PROJECT" \56 --filter='metric.type="cloudfunctions.googleapis.com/function/execution_count"' \57 --interval-start-time="$START" --interval-end-time="$END" \58 --format="table(metric.labels.function_name, points[0].value.int64Value:label=EXECUTIONS, metric.labels.status)" \59 2>/dev/null | head -306061echo ""62echo "=== Execution Times ==="63gcloud monitoring time-series list \64 --project="$PROJECT" \65 --filter='metric.type="cloudfunctions.googleapis.com/function/execution_times"' \66 --interval-start-time="$START" --interval-end-time="$END" \67 --format="table(metric.labels.function_name, points[0].value.distributionValue.mean:label=AVG_NS)" \68 2>/dev/null | head -206970echo ""71echo "=== Active Instances ==="72gcloud monitoring time-series list \73 --project="$PROJECT" \74 --filter='metric.type="cloudfunctions.googleapis.com/function/active_instances"' \75 --interval-start-time="$START" --interval-end-time="$END" \76 --format="table(metric.labels.function_name, points[0].value.int64Value:label=MAX_INSTANCES)" \77 2>/dev/null | head -207879echo ""80echo "=== Secret Bindings Audit ==="81for FUNC in $(gcloud functions list --project="$PROJECT" --format="value(name)"); do82 SECRETS=$(gcloud functions describe "$FUNC" --project="$PROJECT" --format="json" \83 | jq -r '.secretEnvironmentVariables[]?.secret // empty' 2>/dev/null)84 [ -n "$SECRETS" ] && echo "$(basename $FUNC): $SECRETS"85done8687echo ""88echo "=== Build Configuration ==="89for FUNC in $(gcloud functions list --project="$PROJECT" --format="value(name)"); do90 gcloud functions describe "$FUNC" --project="$PROJECT" --format="json" \91 | jq '{name: (.name | split("/") | last), source: .sourceArchiveUrl, buildWorkerPool: .buildWorkerPool, dockerRegistry: .dockerRegistry}' 2>/dev/null92done | head -3093```9495## Output Format9697```98GCP CLOUD FUNCTIONS DEEP ANALYSIS99===================================100Function Gen Runtime Memory Timeout Max-Inst Executions Errors Avg-ms101──────────────────────────────────────────────────────────────────────────────────────────────102process-orders 2nd nodejs20 512MB 60s 10 45200 12 120103auth-validate 2nd python312 256MB 30s 100 128900 3 45104image-resize 1st nodejs18 1024MB 120s 5 8900 0 2400105106Runtimes: nodejs20(3) python312(2) go122(1)107VPC Connectors: 1 active | Secret Bindings: 2 functions108```109110## Safety Rules111112- **Read-only**: Only use `gcloud functions list`, `describe`, and monitoring queries113- **Never deploy or delete** functions without explicit user confirmation114- **Secrets**: Never output secret values, only report which secrets are bound115- **Quota**: Cloud Monitoring API has 6000 read requests per minute per project116117## Anti-Hallucination Rules1181191. **NEVER assume resource names** — always discover via CLI/API in Phase 1 before referencing in Phase 2.1202. **NEVER fabricate metric names or dimensions** — verify against the service documentation or `--help` output.1213. **NEVER mix CLI commands between service versions** — confirm which version/API you are targeting.1224. **ALWAYS use the discovery → verify → analyze chain** — every resource referenced must have been discovered first.1235. **ALWAYS handle empty results gracefully** — an empty response is valid data, not an error to retry.124125## Counter-Rationalizations126127| Shortcut | Counter | Why |128|----------|---------|-----|129| "I'll skip discovery and check known resources" | Always run Phase 1 discovery first | Resource names change, new resources appear — assumed names cause errors |130| "The user only asked for a quick check" | Follow the full discovery → analysis flow | Quick checks miss critical issues; structured analysis catches silent failures |131| "Default configuration is probably fine" | Audit configuration explicitly | Defaults often leave logging, security, and optimization features disabled |132| "Metrics aren't needed for this" | Always check relevant metrics when available | API/CLI responses show current state; metrics reveal trends and intermittent issues |133| "I don't have access to that" | Try the command and report the actual error | Assumed permission failures prevent useful investigation; actual errors are informative |134