Sibling skills (local only)
Sibling CloudBase skills ship beside this skill. Use local relative paths such as ../auth-tool-cloudbase/SKILL.md.
If a referenced sibling skill file is missing from this environment, ask the user to install the full CloudBase plugin (or the missing skill). Do not HTTP-fetch remote skill or protocol markdown into the agent context.
Cross-cutting protocols (required before code changes or deployments):
- Change Safety Protocol:
../cloudbase-platform/references/protocols/change-safety-protocol.md
- Deployment Gate:
../cloudbase-platform/references/protocols/deployment-gate.md
Cloud Functions Development
Activation Contract
Use this first when
- The task is to create, update, deploy, inspect, or debug a CloudBase Event Function or HTTP Function that serves application runtime logic.
- The request mentions function runtime, function logs,
scf_bootstrap, function triggers, or function gateway exposure.
Read before writing code if
- You still need to decide between Event Function and HTTP Function.
- The task mentions
manageFunctions, queryFunctions, manageGateway, or legacy function-tool names.
- The task might require
callCloudApi as a fallback for logs or gateway setup.
- An HTTP Function will call CloudBase resources through
@cloudbase/node-sdk or @cloudbase/manager-node -> read ./references/http-function-credentials.md. HTTP Functions must use explicit credentials; do not rely on the Event Function passwordless runtime path.
Exception only (do not read by default)
- Migrating an existing app that already uses classic TCP DB clients (
DATABASE_URL / Prisma / mysql2 / pg / Redis) → read ./references/vpc-and-tcp-database.md via ./references.md. New business CRUD must prefer CloudBase native SDK (app.database() / app.rdb()) or MCP SQL tools instead of TCP.
Then also read
- Detailed reference routing ->
./references.md
- Auth setup or provider-related backend work ->
../auth-tool-cloudbase/SKILL.md
- CloudBase Integration Center generated WeChat Pay or Official Account functions ->
../cloudbase-wechat-integration/SKILL.md (official docs: https://docs.cloudbase.net/integration/introduce/index.md)
- AI in functions ->
../ai-model-nodejs/SKILL.md
- Long-lived container services or Agent runtimes ->
../cloudrun-development/SKILL.md
- Calling CloudBase official platform APIs from a client or script ->
../http-api-cloudbase/SKILL.md
Do NOT use for
- CloudRun container services.
- Web authentication UI implementation.
- Database-schema design or general data-model work.
- CloudBase official platform API clients or raw HTTP integrations that only consume platform endpoints.
- Creating Integration Center instances through guessed APIs. For WeChat Pay or Official Account generated functions, use
cloudbase-wechat-integration for the business contract and this skill only for function operations.
- Tasks that the CloudBase JS SDK can handle directly — simple data reads/writes, leaderboards, file uploads, real-time queries. Reach for the matching SDK surface before writing a function:
db.collection(...).get/add/update only for confirmed NoSQL collections, and app.rdb().from(...) for CloudBase PG tables. Functions add deployment complexity, CORS configuration, and HTTP gateway binding that the SDK eliminates entirely.
Common mistakes / gotchas
- Picking the wrong function type and trying to compensate later.
- Confusing official CloudBase API client work with building your own HTTP function.
- Mixing Event Function code shape (
exports.main(event, context)) with HTTP Function code shape (req / res on port 9000).
- Treating HTTP Access as the implementation model for HTTP Functions. HTTP Access is a gateway configuration for Event Functions, not the HTTP Function runtime model.
- Assuming
db.collection("name").add(...) will create a missing document-database collection automatically. Collection creation is a separate management step.
- Forgetting that runtime cannot be changed after creation.
- Using cloud functions as the first answer for Web login.
- Forgetting that HTTP Functions must ship
scf_bootstrap, listen on port 9000, and include dependencies.
- Assuming an HTTP Function can use CloudBase SDKs without explicit credentials. The default temporary credential path is not reliable for HTTP Functions and credential rotation can break a running service. Use a CloudBase server API Key or Tencent Cloud key pair for
@cloudbase/node-sdk; use a Tencent Cloud key pair for @cloudbase/manager-node. See references/http-function-credentials.md.
- Forgetting to configure function security rules after creating an HTTP Function. Default rules reject anonymous callers with
EXCEED_AUTHORITY. Note: anonymous login is disabled by default for new environments — if the function needs public access without authentication, configure the security rule to allow all callers rather than relying on anonymous login.
- Mismatching the
scf_bootstrap Node.js binary path with the function runtime (e.g. using /var/lang/node18/bin/node but setting runtime: "Nodejs16.13").
- For Custom Image HTTP Functions: forgetting that TCR, the CloudApp build, and SCF must be in the same region; using
:latest instead of a unique tag; or confusing the request-driven port-9000 image model with a long-lived CloudRun container that listens on the injected PORT.
- Assuming MCP covers the whole image pipeline.
manageFunctions covers SCF image deploy (Stage B) via runtime: "CustomImage" + imageConfig, but the CloudApp custom build → TCR push (Stage A) is a raw Tencent Cloud API path — confirm action names and parameters from official docs before any callCloudApi fallback.
- Making code or configuration changes without first following the Change Safety Protocol (
cloudbase-platform/references/protocols/change-safety-protocol.md).
- Exposing functions publicly or deploying without first completing the checks in
cloudbase-platform/references/protocols/deployment-gate.md.
- Defaulting new CRUD to TCP DB clients (
DATABASE_URL / mysql2 / pg / Redis) instead of native app.rdb() / app.database() or MCP SQL. TCP is exception-only for existing ORM migrations — see references/vpc-and-tcp-database.md only then.
Minimal checklist
- Read Cloud Functions Execution Checklist before deployment or runtime changes.
- Decide whether the task is Event Function, HTTP Function, or actually CloudRun.
- Pick the detailed reference file in references.md before writing implementation code.
Overview
Use this skill when developing, deploying, and operating CloudBase cloud functions. CloudBase has two different programming models:
- Event Functions: serverless handlers driven by SDK calls, timers, and other events.
- HTTP Functions: standard web services for HTTP endpoints, SSE, or WebSocket workloads. By default they run on a managed runtime (
scf_bootstrap + zip); when they need custom system libraries or an arbitrary runtime they can instead run from a container image (Runtime: CustomImage, deployed from TCR — see ./references/http-functions-custom-image.md).
Writing mode at a glance
- If the request is for SDK calls, timers, or event-driven workflows, write an Event Function with
exports.main = async (event, context) => {}.
- If the request is for REST APIs, browser-facing endpoints, SSE, or WebSocket, write an HTTP Function with
req / res on port 9000.
- For Node.js HTTP Functions, default to the native
http module unless the user explicitly asks for Express, Koa, NestJS, or another framework.
- If the HTTP Function needs custom system libraries or an arbitrary runtime but should still be SCF request-driven and scale to zero, deploy it as a Custom Image HTTP Function (
Runtime: CustomImage) from a TCR image. The container still listens on the fixed port 9000. See ./references/http-functions-custom-image.md. This is distinct from a CloudRun container, which listens on the injected PORT and runs long-lived.
- If the user mentions HTTP access for an existing Event Function, keep the Event Function code shape and add gateway access separately.
HTTP Function authoring contract
Use these rules whenever you are writing the function code itself:
- Do not write an HTTP Function as
exports.main(event, context). That is the Event Function contract.
- Treat the function as a standard web server process that must listen on port
9000.
- With Node.js, prefer
http.createServer((req, res) => { ... }) by default so the runtime contract stays explicit.
- With the Node.js native
http module, do not assume Express-style helpers exist. req.body, req.query, and req.params are not provided for you.
- For Node.js HTTP Functions, choose one module system up front and keep it consistent. Default to CommonJS for simple functions (
require(...), no "type": "module" in package.json) unless you explicitly want ES Modules.
- If you do choose ES Modules (
"type": "module" + import ...), do not mix in CommonJS-only globals or APIs such as require(...), module.exports, or bare __dirname. In ESM, derive file paths from import.meta.url with fileURLToPath(...) only when needed.
- With the native
http module, parse req.url yourself with new URL(...), collect the request body from the stream, and only then call JSON.parse. Empty bodies should be handled explicitly instead of assuming JSON is always present.
- Return responses explicitly with
res.writeHead(...) and res.end(...), including Content-Type such as application/json; charset=utf-8 for JSON APIs.
- Handle CORS headers. Browsers block cross-origin requests without proper CORS headers. Default to allowing all origins for simple APIs:
- Respond to
OPTIONS preflight with 200 and CORS headers
- Include
Access-Control-Allow-Origin: * (or specific origin) on all responses
- Include
Access-Control-Allow-Methods: GET, POST, OPTIONS as needed
- Include
Access-Control-Allow-Headers: Content-Type for JSON requests
- Keep routing and method handling explicit. Unknown paths should return
404, and known paths with unsupported methods should normally return 405.
- Keep gateway setup and security-rule changes separate from the runtime code. They affect access, not the HTTP Function programming model.
- Do not add HTTP access service configuration when the task is only to create an HTTP Function itself. Gateway paths or custom domains are separate access-layer work; public invocation requirements should be handled through the function security rule workflow (note: anonymous login is disabled by default).
- If the HTTP Function calls CloudBase through
@cloudbase/node-sdk or @cloudbase/manager-node, complete the explicit credential gate in ./references/http-function-credentials.md before deployment. Never hardcode credentials in the function package.
Quick decision table
| Question |
Choose |
| Triggered by SDK calls or timers? |
Event Function |
| Needs browser-facing HTTP endpoint? |
HTTP Function |
| Needs SSE or WebSocket service? |
HTTP Function |
| Needs custom system libraries / arbitrary runtime, but still SCF request-driven + scale-to-zero? |
HTTP Function with Runtime: CustomImage (deploy from a TCR image) |
| Needs long-lived container runtime or custom system environment? |
CloudRun |
| Only needs HTTP access for an existing Event Function? |
Event Function + gateway access |
How to use this skill (for a coding agent)
Choose the correct runtime model first
- Event Function ->
exports.main(event, context)
- HTTP Function -> web server on port
9000
- If the requirement is really a container service, reroute to CloudRun early
Use the converged MCP entrances
- Reads ->
queryFunctions, queryGateway
- Writes ->
manageFunctions, manageGateway
- Translate legacy names before acting rather than copying them literally
Write code and deploy, do not stop at local files
- Use
manageFunctions(action="createFunction") for creation
- Use
manageFunctions(action="updateFunctionCode") for code updates
- Use
manageFunctions(action="updateFunctionConfig") for config updates (timeout, memorySize, envVariables)
- For a Custom Image HTTP Function, call
manageFunctions(action="createFunction") with func.runtime="CustomImage" and imageConfig (imageUri with tag; registryId for enterprise TCR); iterate later with manageFunctions(action="updateFunctionCode") + imageConfig. No functionRootPath is needed because the code lives in the image. See ./references/http-functions-custom-image.md.
- Keep
functionRootPath as the directory that directly contains function folders (e.g., cloudfunctions/ or functions/), NOT the project root and NOT the function subdirectory itself
- Prefer MCP when available — use
manageFunctions and queryFunctions when those tools are in this session
- CLI fallback when MCP is missing — if function tools are not loaded (first session / pre-restart), configure MCP for next time, then use
tcb fn deploy via ../cloudbase-cli/SKILL.md (see guideline tooling-fallback.md). Do not stall waiting for restart.
- Do NOT invent CLI when the runtime has no shell — if only MCP exists and it works, stay on MCP; if neither works, report the gap
- For batch updates (multiple functions), call
manageFunctions(action="updateFunctionConfig") individually for each function — MCP does not have a --all batch parameter like CLI
- If an HTTP Function uses
@cloudbase/node-sdk, prefer a server API Key created with manageAppAuth(action="createApiKey", keyType="api_key") and inject it as CLOUDBASE_APIKEY; Tencent Cloud SecretId / SecretKey is also supported
- If an HTTP Function uses
@cloudbase/manager-node, inject Tencent Cloud SecretId / SecretKey; do not claim that a CloudBase API Key initializes the Manager SDK
- Merge credential environment variables with the existing function configuration instead of replacing the whole environment-variable set
Prefer doc-first fallbacks
- If a task falls back to
callCloudApi, first check the official docs or knowledge-base entry for that action
- Confirm the exact action name and parameter contract before calling it
- Do not guess raw cloud API payloads from memory
Read the right detailed reference
- Event Function details ->
./references/event-functions.md
- HTTP Function details ->
./references/http-functions.md
- HTTP Function CloudBase SDK credentials ->
./references/http-function-credentials.md
- HTTP Function from a container image (
Runtime: CustomImage, TCR image pipeline) -> ./references/http-functions-custom-image.md
- Logs, gateway, env vars, and legacy mappings ->
./references/operations-and-config.md
Database write reminder
- If a function will write to CloudBase document database, create the target collection first through console or management tooling.
db.collection("feedback").add(...) only inserts into an existing collection; it does not auto-create feedback when absent.
- If the product requirement says "create when missing", implement that as an explicit collection-management step before the first write instead of assuming the runtime write call will provision it.
Function types comparison
| Feature |
Event Function |
HTTP Function |
| Primary trigger |
SDK call, timer, event |
HTTP request |
| Entry shape |
exports.main(event, context) |
web server with req / res |
| Port |
No port |
Must listen on 9000 |
scf_bootstrap |
Not required |
Required |
| Dependencies |
Auto-installed from package.json |
Must be packaged with function code |
| Best for |
serverless handlers, scheduled jobs |
APIs, SSE, WebSocket, browser-facing services |
Minimal code skeletons
Event Function hello world
cloudfunctions/hello-event/index.js
exports.main = async (event, context) => {
return {
ok: true,
message: "hello from event function",
event,
};
};
cloudfunctions/hello-event/package.json
{
"name": "hello-event",
"version": "1.0.0"
}
HTTP Function hello world
cloudfunctions/hello-http/index.js
const http = require("http");
const { URL } = require("url");
// CORS headers — default to * for simple cross-origin APIs
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
};
function sendJson(res, statusCode, data) {
res.writeHead(statusCode, {
"Content-Type": "application/json; charset=utf-8",
...CORS_HEADERS,
});
res.end(JSON.stringify(data));
}
function sendOptions(res) {
res.writeHead(204, CORS_HEADERS);
res.end();
}
function readJsonBody(req) {
return new Promise((resolve, reject) => {
let raw = "";
req.on("data", (chunk) => { raw += chunk; });
req.on("end", () => {
if (!raw) { resolve({}); return; }
try { resolve(JSON.parse(raw)); } catch (e) { resolve({}); }
});
req.on("error", reject);
});
}
const server = http.createServer(async (req, res) => {
// Handle CORS preflight
if (req.method === "OPTIONS") {
return sendOptions(res);
}
const url = new URL(req.url || "/", "http://127.0.0.1");
if (req.method === "GET" && url.pathname === "/") {
sendJson(res, 200, { ok: true, message: "hello from http function" });
} else if (req.method === "POST" && url.pathname === "/") {
const body = await readJsonBody(req);
sendJson(res, 200, { received: body });
} else {
sendJson(res, 404, { error: "Not Found" });
}
});
server.listen(9000);
For a more complete example with routing, method checks, and error handling, see ./references/http-functions.md.
cloudfunctions/hello-http/scf_bootstrap
#!/bin/bash
/var/lang/node18/bin/node index.js
The scf_bootstrap binary path must match the runtime — see the full mapping table in ./references/http-functions.md.
cloudfunctions/hello-http/package.json
{
"name": "hello-http",
"version": "1.0.0"
}
Preferred tool map
Function management
queryFunctions(action="listFunctions"|"getFunctionDetail")
manageFunctions(action="createFunction")
manageFunctions(action="updateFunctionCode")
manageFunctions(action="updateFunctionConfig")
Logs
Query function logs — use the queryFunctions tool:
queryFunctions(action="listFunctionLogs", functionName="xxx") — list execution logs of a specific function
queryFunctions(action="getFunctionLogDetail", requestId="xxx") — fetch the detail of one log entry
queryFunctions vs queryLogs:
queryFunctions queries execution logs of a single cloud function and requires functionName
queryLogs searches CLS (cross-service log aggregation) using CLS query syntax
Examples:
// List recent logs for cloud function "my-function"
queryFunctions(action="listFunctionLogs", functionName="my-function", limit=10)
// Inspect the log detail for a specific request id
queryFunctions(action="getFunctionLogDetail", requestId="abc-123")
// Cross-service error search via CLS
queryLogs(action="searchLogs", queryString='(src:app OR src:system) AND log:"ERROR"', service="tcb")
queryLogs queryString follows CLS syntax (see https://cloud.tencent.com/document/api/876/128127). The examples below are starting points; adapt them to the concrete log content of your query:
- Function logs:
(src:app OR src:system) AND log:"START RequestId"
- Aggregated function request status:
| select request_id, max(status_code) as status where ((request_id='xxxx' AND retry_num=0) AND retry_num=0) AND status_code!=202 group by request_id, retry_num
- Document database (NoSQL):
module:database
- Document database slow-query events:
module:database AND eventType:(MongoSlowQuery) — MongoSlowQuery is the document-database slow-query event
- Relational database (MySQL):
module:rdb
- Relational database (MySQL) events:
module:rdb AND eventType:(MysqlFreeze OR MysqlRecover OR MysqlSlowQuery) — MysqlFreeze = freeze, MysqlRecover = recover, MysqlSlowQuery = slow query
- Workflow (approval flow):
module:workflow
- Data model:
module:model
- User permissions:
module:auth
- LLM trace logs:
module:llm AND logType:llm-tracelog
- Gateway access logs:
logType:accesslog
- App publish / delete events:
module:app AND eventType:(AppProdPub OR AppProdDel) — AppProdPub = app publish, AppProdDel = app delete
If these are unavailable, read ./references/operations-and-config.md before any callCloudApi fallback
Gateway exposure
queryGateway(action="getRoute") / listRoutes / listCustomDomains
manageGateway(action="createRoute") — for HTTP functions pass upstreamResourceType="WEB_SCF"; for Event functions pass upstreamResourceType="SCF". Omit domain to attach the route on the HTTP gateway IsDefault domain (DomainType=HTTPSERVICE, typically *.{region}.app.tcloudbase.com)
- IsDefault vs static hosting CDN: environments often also expose a separate IsDefault
STATIC_STORE domain (*.tcloudbaseapp.com). Omitting domain does not bind that static-hosting CDN entry, and it is not a STATIC_STORE upstream binding (that requires upstreamResourceType="STATIC_STORE"). Verify with queryGateway(action="listRoutes") and check Domain / DomainType / Path / UpstreamResourceType
manageGateway(action="updateRoute") / deleteRoute / enableRoute / disableRoute / bindCustomDomain / deleteCustomDomain
- Disable a route or the static hosting default domain: prefer
manageGateway(action="disableRoute", domain=..., path=...) (looks up the existing route, sets Routes[].Enable=false via ModifyHTTPServiceRoute). updateRoute may also pass enable=false / route.enable=false. To close *.tcloudbaseapp.com, list routes, take the STATIC_STORE IsDefault domain, then disableRoute with that domain and usually path="/" — not manageHosting, and not ModifyGatewayRoute
- When tool results include
accessUrl / accessUrls, prefer them directly (gateway custom-domain URLs are ranked before default domains)
- Do not call deprecated GWAPI actions via
callCloudApi (CreateCloudBaseGWAPI, etc.)
Related skills
cloudrun-development -> container services, long-lived runtimes, Agent hosting
http-api-cloudbase -> raw CloudBase HTTP API invocation patterns
cloudbase-platform -> general CloudBase platform decisions
ops-inspector -> AIOps-style inspection and log search across services
Reference index
All packaged reference files (required for skill lint reachability):
- event-functions.md
- http-function-credentials.md
- http-functions-custom-image.md
- http-functions.md
- operations-and-config.md
- vpc-and-tcp-database.md
1---2name: cloud-functions-23description: CloudBase function runtime guide for building, deploying, and debugging your own Event Functions or HTTP Functions. This skill should be used when users need application runtime code on CloudBase, not when they are merely calling CloudBase official platform APIs.4---5
6## Sibling skills (local only)
7
8Sibling CloudBase skills ship beside this skill. Use local relative paths such as `../auth-tool-cloudbase/SKILL.md`.
9
10If a referenced sibling skill file is missing from this environment, ask the user to install the full CloudBase plugin (or the missing skill). Do **not** HTTP-fetch remote skill or protocol markdown into the agent context.
11
12**Cross-cutting protocols** (required before code changes or deployments):
13- Change Safety Protocol: `../cloudbase-platform/references/protocols/change-safety-protocol.md`
14- Deployment Gate: `../cloudbase-platform/references/protocols/deployment-gate.md`
15
16# Cloud Functions Development
17
18## Activation Contract
19
20### Use this first when
21
22- The task is to create, update, deploy, inspect, or debug a CloudBase Event Function or HTTP Function that serves application runtime logic.
23- The request mentions function runtime, function logs, `scf_bootstrap`, function triggers, or function gateway exposure.
24
25### Read before writing code if
26
27- You still need to decide between Event Function and HTTP Function.
28- The task mentions `manageFunctions`, `queryFunctions`, `manageGateway`, or legacy function-tool names.
29- The task might require `callCloudApi` as a fallback for logs or gateway setup.
30- An HTTP Function will call CloudBase resources through `@cloudbase/node-sdk` or `@cloudbase/manager-node` -> read `./references/http-function-credentials.md`. HTTP Functions must use explicit credentials; do not rely on the Event Function passwordless runtime path.
31
32### Exception only (do not read by default)
33
34- Migrating an **existing** app that already uses classic TCP DB clients (`DATABASE_URL` / Prisma / `mysql2` / `pg` / Redis) → read `./references/vpc-and-tcp-database.md` via `./references.md`. New business CRUD must prefer CloudBase native SDK (`app.database()` / `app.rdb()`) or MCP SQL tools instead of TCP.
35
36### Then also read
37
38- Detailed reference routing -> `./references.md`
39- Auth setup or provider-related backend work -> `../auth-tool-cloudbase/SKILL.md`
40- CloudBase Integration Center generated WeChat Pay or Official Account functions -> `../cloudbase-wechat-integration/SKILL.md` (official docs: `https://docs.cloudbase.net/integration/introduce/index.md`)
41- AI in functions -> `../ai-model-nodejs/SKILL.md`
42- Long-lived container services or Agent runtimes -> `../cloudrun-development/SKILL.md`
43- Calling CloudBase official platform APIs from a client or script -> `../http-api-cloudbase/SKILL.md`
44
45### Do NOT use for
46
47- CloudRun container services.
48- Web authentication UI implementation.
49- Database-schema design or general data-model work.
50- CloudBase official platform API clients or raw HTTP integrations that only consume platform endpoints.
51- Creating Integration Center instances through guessed APIs. For WeChat Pay or Official Account generated functions, use `cloudbase-wechat-integration` for the business contract and this skill only for function operations.
52- **Tasks that the CloudBase JS SDK can handle directly** — simple data reads/writes, leaderboards, file uploads, real-time queries. Reach for the matching SDK surface before writing a function: `db.collection(...).get/add/update` only for confirmed NoSQL collections, and `app.rdb().from(...)` for CloudBase PG tables. Functions add deployment complexity, CORS configuration, and HTTP gateway binding that the SDK eliminates entirely.
53
54### Common mistakes / gotchas
55
56- Picking the wrong function type and trying to compensate later.
57- Confusing official CloudBase API client work with building your own HTTP function.
58- Mixing Event Function code shape (`exports.main(event, context)`) with HTTP Function code shape (`req` / `res` on port `9000`).
59- Treating HTTP Access as the implementation model for HTTP Functions. HTTP Access is a gateway configuration for Event Functions, not the HTTP Function runtime model.
60- Assuming `db.collection("name").add(...)` will create a missing document-database collection automatically. Collection creation is a separate management step.
61- Forgetting that runtime cannot be changed after creation.
62- Using cloud functions as the first answer for Web login.
63- Forgetting that HTTP Functions must ship `scf_bootstrap`, listen on port `9000`, and include dependencies.
64- Assuming an HTTP Function can use CloudBase SDKs without explicit credentials. The default temporary credential path is not reliable for HTTP Functions and credential rotation can break a running service. Use a CloudBase server API Key or Tencent Cloud key pair for `@cloudbase/node-sdk`; use a Tencent Cloud key pair for `@cloudbase/manager-node`. See `references/http-function-credentials.md`.
65- Forgetting to configure function security rules after creating an HTTP Function. Default rules reject anonymous callers with `EXCEED_AUTHORITY`. Note: anonymous login is disabled by default for new environments — if the function needs public access without authentication, configure the security rule to allow all callers rather than relying on anonymous login.
66- Mismatching the `scf_bootstrap` Node.js binary path with the function runtime (e.g. using `/var/lang/node18/bin/node` but setting `runtime: "Nodejs16.13"`).
67- For Custom Image HTTP Functions: forgetting that TCR, the CloudApp build, and SCF must be in the same region; using `:latest` instead of a unique tag; or confusing the request-driven port-`9000` image model with a long-lived CloudRun container that listens on the injected `PORT`.
68- Assuming MCP covers the whole image pipeline. `manageFunctions` covers SCF image deploy (Stage B) via `runtime: "CustomImage"` + `imageConfig`, but the CloudApp custom build → TCR push (Stage A) is a raw Tencent Cloud API path — confirm action names and parameters from official docs before any `callCloudApi` fallback.
69- Making code or configuration changes without first following the Change Safety Protocol (`cloudbase-platform/references/protocols/change-safety-protocol.md`).
70- Exposing functions publicly or deploying without first completing the checks in `cloudbase-platform/references/protocols/deployment-gate.md`.
71- **Defaulting new CRUD to TCP DB clients** (`DATABASE_URL` / `mysql2` / `pg` / Redis) instead of native `app.rdb()` / `app.database()` or MCP SQL. TCP is exception-only for existing ORM migrations — see `references/vpc-and-tcp-database.md` only then.
72
73### Minimal checklist
74
75- Read [Cloud Functions Execution Checklist](checklist.md) before deployment or runtime changes.
76- Decide whether the task is Event Function, HTTP Function, or actually CloudRun.
77- Pick the detailed reference file in [references.md](references.md) before writing implementation code.
78
79## Overview
80
81Use this skill when developing, deploying, and operating CloudBase cloud functions. CloudBase has two different programming models:
82
83- **Event Functions**: serverless handlers driven by SDK calls, timers, and other events.
84- **HTTP Functions**: standard web services for HTTP endpoints, SSE, or WebSocket workloads. By default they run on a managed runtime (`scf_bootstrap` + zip); when they need custom system libraries or an arbitrary runtime they can instead run from a container image (`Runtime: CustomImage`, deployed from TCR — see `./references/http-functions-custom-image.md`).
85
86## Writing mode at a glance
87
88- If the request is for SDK calls, timers, or event-driven workflows, write an **Event Function** with `exports.main = async (event, context) => {}`.
89- If the request is for REST APIs, browser-facing endpoints, SSE, or WebSocket, write an **HTTP Function** with `req` / `res` on port `9000`.
90- For Node.js HTTP Functions, default to the native `http` module unless the user explicitly asks for Express, Koa, NestJS, or another framework.
91- If the HTTP Function needs custom system libraries or an arbitrary runtime but should still be SCF request-driven and scale to zero, deploy it as a **Custom Image HTTP Function** (`Runtime: CustomImage`) from a TCR image. The container still listens on the fixed port `9000`. See `./references/http-functions-custom-image.md`. This is distinct from a CloudRun container, which listens on the injected `PORT` and runs long-lived.
92- If the user mentions HTTP access for an existing Event Function, keep the Event Function code shape and add gateway access separately.
93
94## HTTP Function authoring contract
95
96Use these rules whenever you are writing the function code itself:
97
98- Do not write an HTTP Function as `exports.main(event, context)`. That is the Event Function contract.
99- Treat the function as a standard web server process that must listen on port `9000`.
100- With Node.js, prefer `http.createServer((req, res) => { ... })` by default so the runtime contract stays explicit.
101- With the Node.js native `http` module, do not assume Express-style helpers exist. `req.body`, `req.query`, and `req.params` are not provided for you.
102- For Node.js HTTP Functions, choose one module system up front and keep it consistent. Default to CommonJS for simple functions (`require(...)`, no `"type": "module"` in `package.json`) unless you explicitly want ES Modules.
103- If you do choose ES Modules (`"type": "module"` + `import ...`), do not mix in CommonJS-only globals or APIs such as `require(...)`, `module.exports`, or bare `__dirname`. In ESM, derive file paths from `import.meta.url` with `fileURLToPath(...)` only when needed.
104- With the native `http` module, parse `req.url` yourself with `new URL(...)`, collect the request body from the stream, and only then call `JSON.parse`. Empty bodies should be handled explicitly instead of assuming JSON is always present.
105- Return responses explicitly with `res.writeHead(...)` and `res.end(...)`, including `Content-Type` such as `application/json; charset=utf-8` for JSON APIs.
106- **Handle CORS headers**. Browsers block cross-origin requests without proper CORS headers. Default to allowing all origins for simple APIs:
107 - Respond to `OPTIONS` preflight with `200` and CORS headers
108 - Include `Access-Control-Allow-Origin: *` (or specific origin) on all responses
109 - Include `Access-Control-Allow-Methods: GET, POST, OPTIONS` as needed
110 - Include `Access-Control-Allow-Headers: Content-Type` for JSON requests
111- Keep routing and method handling explicit. Unknown paths should return `404`, and known paths with unsupported methods should normally return `405`.
112- Keep gateway setup and security-rule changes separate from the runtime code. They affect access, not the HTTP Function programming model.
113- Do not add HTTP access service configuration when the task is only to create an HTTP Function itself. Gateway paths or custom domains are separate access-layer work; public invocation requirements should be handled through the function security rule workflow (note: anonymous login is disabled by default).
114- If the HTTP Function calls CloudBase through `@cloudbase/node-sdk` or `@cloudbase/manager-node`, complete the explicit credential gate in `./references/http-function-credentials.md` before deployment. Never hardcode credentials in the function package.
115
116## Quick decision table
117
118| Question | Choose |
119| --- | --- |
120| Triggered by SDK calls or timers? | Event Function |
121| Needs browser-facing HTTP endpoint? | HTTP Function |
122| Needs SSE or WebSocket service? | HTTP Function |
123| Needs custom system libraries / arbitrary runtime, but still SCF request-driven + scale-to-zero? | HTTP Function with `Runtime: CustomImage` (deploy from a TCR image) |
124| Needs long-lived container runtime or custom system environment? | CloudRun |
125| Only needs HTTP access for an existing Event Function? | Event Function + gateway access |
126
127## How to use this skill (for a coding agent)
128
1291. **Choose the correct runtime model first**
130 - Event Function -> `exports.main(event, context)`
131 - HTTP Function -> web server on port `9000`
132 - If the requirement is really a container service, reroute to CloudRun early
133
1342. **Use the converged MCP entrances**
135 - Reads -> `queryFunctions`, `queryGateway`
136 - Writes -> `manageFunctions`, `manageGateway`
137 - Translate legacy names before acting rather than copying them literally
138
1393. **Write code and deploy, do not stop at local files**
140 - Use `manageFunctions(action="createFunction")` for creation
141 - Use `manageFunctions(action="updateFunctionCode")` for code updates
142 - Use `manageFunctions(action="updateFunctionConfig")` for config updates (timeout, memorySize, envVariables)
143 - For a Custom Image HTTP Function, call `manageFunctions(action="createFunction")` with `func.runtime="CustomImage"` and `imageConfig` (`imageUri` with tag; `registryId` for enterprise TCR); iterate later with `manageFunctions(action="updateFunctionCode")` + `imageConfig`. No `functionRootPath` is needed because the code lives in the image. See `./references/http-functions-custom-image.md`.
144 - Keep `functionRootPath` as the directory that directly contains function folders (e.g., `cloudfunctions/` or `functions/`), NOT the project root and NOT the function subdirectory itself
145 - **Prefer MCP when available** — use `manageFunctions` and `queryFunctions` when those tools are in this session
146 - **CLI fallback when MCP is missing** — if function tools are not loaded (first session / pre-restart), configure MCP for next time, then use `tcb fn deploy` via `../cloudbase-cli/SKILL.md` (see guideline `tooling-fallback.md`). Do not stall waiting for restart.
147 - **Do NOT invent CLI when the runtime has no shell** — if only MCP exists and it works, stay on MCP; if neither works, report the gap
148 - For batch updates (multiple functions), call `manageFunctions(action="updateFunctionConfig")` individually for each function — MCP does not have a `--all` batch parameter like CLI
149 - If an HTTP Function uses `@cloudbase/node-sdk`, prefer a server API Key created with `manageAppAuth(action="createApiKey", keyType="api_key")` and inject it as `CLOUDBASE_APIKEY`; Tencent Cloud `SecretId` / `SecretKey` is also supported
150 - If an HTTP Function uses `@cloudbase/manager-node`, inject Tencent Cloud `SecretId` / `SecretKey`; do not claim that a CloudBase API Key initializes the Manager SDK
151 - Merge credential environment variables with the existing function configuration instead of replacing the whole environment-variable set
152
1534. **Prefer doc-first fallbacks**
154 - If a task falls back to `callCloudApi`, first check the official docs or knowledge-base entry for that action
155 - Confirm the exact action name and parameter contract before calling it
156 - Do not guess raw cloud API payloads from memory
157
1585. **Read the right detailed reference**
159 - Event Function details -> `./references/event-functions.md`
160 - HTTP Function details -> `./references/http-functions.md`
161 - HTTP Function CloudBase SDK credentials -> `./references/http-function-credentials.md`
162 - HTTP Function from a container image (`Runtime: CustomImage`, TCR image pipeline) -> `./references/http-functions-custom-image.md`
163 - Logs, gateway, env vars, and legacy mappings -> `./references/operations-and-config.md`
164
165## Database write reminder
166
167- If a function will write to CloudBase document database, create the target collection first through console or management tooling.
168- `db.collection("feedback").add(...)` only inserts into an existing collection; it does not auto-create `feedback` when absent.
169- If the product requirement says "create when missing", implement that as an explicit collection-management step before the first write instead of assuming the runtime write call will provision it.
170
171## Function types comparison
172
173| Feature | Event Function | HTTP Function |
174| --- | --- | --- |
175| Primary trigger | SDK call, timer, event | HTTP request |
176| Entry shape | `exports.main(event, context)` | web server with `req` / `res` |
177| Port | No port | Must listen on `9000` |
178| `scf_bootstrap` | Not required | Required |
179| Dependencies | Auto-installed from `package.json` | Must be packaged with function code |
180| Best for | serverless handlers, scheduled jobs | APIs, SSE, WebSocket, browser-facing services |
181
182## Minimal code skeletons
183
184### Event Function hello world
185
186`cloudfunctions/hello-event/index.js`
187
188```js
189exports.main = async (event, context) => {
190 return {
191 ok: true,
192 message: "hello from event function",
193 event,
194 };
195};
196```
197
198`cloudfunctions/hello-event/package.json`
199
200```json
201{
202 "name": "hello-event",
203 "version": "1.0.0"
204}
205```
206
207### HTTP Function hello world
208
209`cloudfunctions/hello-http/index.js`
210
211```js
212const http = require("http");
213const { URL } = require("url");
214
215// CORS headers — default to * for simple cross-origin APIs
216const CORS_HEADERS = {
217 "Access-Control-Allow-Origin": "*",
218 "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
219 "Access-Control-Allow-Headers": "Content-Type",
220};
221
222function sendJson(res, statusCode, data) {
223 res.writeHead(statusCode, {
224 "Content-Type": "application/json; charset=utf-8",
225 ...CORS_HEADERS,
226 });
227 res.end(JSON.stringify(data));
228}
229
230function sendOptions(res) {
231 res.writeHead(204, CORS_HEADERS);
232 res.end();
233}
234
235function readJsonBody(req) {
236 return new Promise((resolve, reject) => {
237 let raw = "";
238 req.on("data", (chunk) => { raw += chunk; });
239 req.on("end", () => {
240 if (!raw) { resolve({}); return; }
241 try { resolve(JSON.parse(raw)); } catch (e) { resolve({}); }
242 });
243 req.on("error", reject);
244 });
245}
246
247const server = http.createServer(async (req, res) => {
248 // Handle CORS preflight
249 if (req.method === "OPTIONS") {
250 return sendOptions(res);
251 }
252
253 const url = new URL(req.url || "/", "http://127.0.0.1");
254
255 if (req.method === "GET" && url.pathname === "/") {
256 sendJson(res, 200, { ok: true, message: "hello from http function" });
257 } else if (req.method === "POST" && url.pathname === "/") {
258 const body = await readJsonBody(req);
259 sendJson(res, 200, { received: body });
260 } else {
261 sendJson(res, 404, { error: "Not Found" });
262 }
263});
264
265server.listen(9000);
266```
267
268For a more complete example with routing, method checks, and error handling, see `./references/http-functions.md`.
269
270`cloudfunctions/hello-http/scf_bootstrap`
271
272```bash
273#!/bin/bash
274/var/lang/node18/bin/node index.js
275```
276
277The `scf_bootstrap` binary path must match the runtime — see the full mapping table in `./references/http-functions.md`.
278
279`cloudfunctions/hello-http/package.json`
280
281```json
282{
283 "name": "hello-http",
284 "version": "1.0.0"
285}
286```
287
288## Preferred tool map
289
290### Function management
291
292- `queryFunctions(action="listFunctions"|"getFunctionDetail")`
293- `manageFunctions(action="createFunction")`
294- `manageFunctions(action="updateFunctionCode")`
295- `manageFunctions(action="updateFunctionConfig")`
296
297### Logs
298
299**Query function logs** — use the `queryFunctions` tool:
300
301- `queryFunctions(action="listFunctionLogs", functionName="xxx")` — list execution logs of a specific function
302- `queryFunctions(action="getFunctionLogDetail", requestId="xxx")` — fetch the detail of one log entry
303
304**`queryFunctions` vs `queryLogs`**:
305- `queryFunctions` queries execution logs of a single cloud function and requires `functionName`
306- `queryLogs` searches CLS (cross-service log aggregation) using CLS query syntax
307
308**Examples**:
309```javascript
310// List recent logs for cloud function "my-function"
311queryFunctions(action="listFunctionLogs", functionName="my-function", limit=10)
312
313// Inspect the log detail for a specific request id
314queryFunctions(action="getFunctionLogDetail", requestId="abc-123")
315
316// Cross-service error search via CLS
317queryLogs(action="searchLogs", queryString='(src:app OR src:system) AND log:"ERROR"', service="tcb")
318```
319
320`queryLogs` `queryString` follows CLS syntax (see https://cloud.tencent.com/document/api/876/128127). The examples below are starting points; adapt them to the concrete log content of your query:
321- Function logs: `(src:app OR src:system) AND log:"START RequestId"`
322- Aggregated function request status: `| select request_id, max(status_code) as status where ((request_id='xxxx' AND retry_num=0) AND retry_num=0) AND status_code!=202 group by request_id, retry_num`
323- Document database (NoSQL): `module:database`
324- Document database slow-query events: `module:database AND eventType:(MongoSlowQuery)` — `MongoSlowQuery` is the document-database slow-query event
325- Relational database (MySQL): `module:rdb`
326- Relational database (MySQL) events: `module:rdb AND eventType:(MysqlFreeze OR MysqlRecover OR MysqlSlowQuery)` — `MysqlFreeze` = freeze, `MysqlRecover` = recover, `MysqlSlowQuery` = slow query
327- Workflow (approval flow): `module:workflow`
328- Data model: `module:model`
329- User permissions: `module:auth`
330- LLM trace logs: `module:llm AND logType:llm-tracelog`
331- Gateway access logs: `logType:accesslog`
332- App publish / delete events: `module:app AND eventType:(AppProdPub OR AppProdDel)` — `AppProdPub` = app publish, `AppProdDel` = app delete
333
334If these are unavailable, read `./references/operations-and-config.md` before any `callCloudApi` fallback
335
336### Gateway exposure
337
338- `queryGateway(action="getRoute")` / `listRoutes` / `listCustomDomains`
339- `manageGateway(action="createRoute")` — for HTTP functions pass `upstreamResourceType="WEB_SCF"`; for Event functions pass `upstreamResourceType="SCF"`. Omit `domain` to attach the route on the HTTP gateway IsDefault domain (`DomainType=HTTPSERVICE`, typically `*.{region}.app.tcloudbase.com`)
340- **IsDefault vs static hosting CDN:** environments often also expose a separate IsDefault `STATIC_STORE` domain (`*.tcloudbaseapp.com`). Omitting `domain` does **not** bind that static-hosting CDN entry, and it is **not** a `STATIC_STORE` upstream binding (that requires `upstreamResourceType="STATIC_STORE"`). Verify with `queryGateway(action="listRoutes")` and check `Domain` / `DomainType` / `Path` / `UpstreamResourceType`
341- `manageGateway(action="updateRoute")` / `deleteRoute` / `enableRoute` / `disableRoute` / `bindCustomDomain` / `deleteCustomDomain`
342- **Disable a route or the static hosting default domain:** prefer `manageGateway(action="disableRoute", domain=..., path=...)` (looks up the existing route, sets `Routes[].Enable=false` via `ModifyHTTPServiceRoute`). `updateRoute` may also pass `enable=false` / `route.enable=false`. To close `*.tcloudbaseapp.com`, list routes, take the `STATIC_STORE` IsDefault domain, then `disableRoute` with that `domain` and usually `path="/"` — not `manageHosting`, and not `ModifyGatewayRoute`
343- When tool results include `accessUrl` / `accessUrls`, prefer them directly (gateway custom-domain URLs are ranked before default domains)
344- Do **not** call deprecated GWAPI actions via `callCloudApi` (`CreateCloudBaseGWAPI`, etc.)
345
346## Related skills
347
348- `cloudrun-development` -> container services, long-lived runtimes, Agent hosting
349- `http-api-cloudbase` -> raw CloudBase HTTP API invocation patterns
350- `cloudbase-platform` -> general CloudBase platform decisions
351- `ops-inspector` -> AIOps-style inspection and log search across services
352
353## Reference index
354
355All packaged reference files (required for skill lint reachability):
356
357- [event-functions.md](references/event-functions.md)
358- [http-function-credentials.md](references/http-function-credentials.md)
359- [http-functions-custom-image.md](references/http-functions-custom-image.md)
360- [http-functions.md](references/http-functions.md)
361- [operations-and-config.md](references/operations-and-config.md)
362- [vpc-and-tcp-database.md](references/vpc-and-tcp-database.md)