Complete guide for CloudBase cloud functions development - runtime selection, deployment, logging, invocation, and HTTP access configuration.
Use this skill when developing, deploying, and managing CloudBase cloud functions (Node.js serverless functions).
Use this skill for cloud function operations when you need to:
Do NOT use for:
cloudrun-development skill)cloudrun-development skill)Understand runtime limitations
Choose the right runtime
Nodejs18.15 (recommended)Deploy functions correctly
createFunction for new functionsupdateFunctionCode for code updates (runtime cannot be changed)functionRootPath (parent directory of function folder)Query logs properly
getFunctionLogs for log list (basic info)getFunctionLogDetail with RequestId for detailed logs⚠️ CRITICAL: Runtime cannot be modified after function creation
Once a cloud function is created with a specific runtime, the runtime cannot be changed. If you need a different runtime:
Supported Node.js Runtimes:
Nodejs18.15 (Default, Recommended)Nodejs16.13Nodejs14.18Nodejs12.16Nodejs10.15Nodejs8.9Runtime Selection Guidelines:
Nodejs18.15 for new projects (default, most modern)Cloud functions require:
Function Directory: Contains function code
index.js (or specified entry file)exports.main = async (event, context) => {}package.json with dependenciesFunction Root Path: Parent directory containing function directories
/project/cloudfunctions/myFunction/functionRootPath should be /project/cloudfunctions/Entry Point: Default is index.js with exports.main
handler parameterCreating New Functions:
Use createFunction tool (see MCP tool documentation for full parameter list):
func.runtime explicitly (defaults to Nodejs18.15)functionRootPath as parent directory of function folders (absolute path)force=true to overwrite existing functionUpdating Function Code:
Use updateFunctionCode tool:
Deployment Best Practices:
functionRootPathQuerying Logs:
Primary Method: Use getFunctionLogs and getFunctionLogDetail tools (see MCP tool documentation).
Alternative Method (Plan B): If tools unavailable, use callCloudApi:
GetFunctionLogs action:callCloudApi({
service: "tcb",
action: "GetFunctionLogs",
params: {
EnvId: "{envId}",
FunctionName: "functionName",
Offset: 0,
Limit: 10,
StartTime: "2024-01-01 00:00:00",
EndTime: "2024-01-01 23:59:59",
LogRequestId: "optional-request-id",
Qualifier: "$LATEST"
}
})
GetFunctionLogDetail action (requires LogRequestId from step 1):callCloudApi({
service: "tcb",
action: "GetFunctionLogDetail",
params: {
StartTime: "2024-01-01 00:00:00",
EndTime: "2024-01-01 23:59:59",
LogRequestId: "request-id-from-log-list"
}
})
Log Query Limitations:
Offset + Limit cannot exceed 10000StartTime and EndTime interval cannot exceed 1 dayLog Query Best Practices:
From Web Applications:
import cloudbase from "@cloudbase/js-sdk";
import cloudbaseSDK from "@cloudbase/js-sdk";
const cloudbase = cloudbaseSDK.init({
env: 'your-env-id',
region: 'ap-shanghai',
accessKey: 'your-access-key'
});
// Call cloud function
const result = await cloudbase.callFunction({
name: "functionName",
data: { /* function parameters */ }
});
From Mini Programs:
wx.cloud.callFunction({
name: "functionName",
data: { /* function parameters */ }
}).then(res => {
console.log(res.result);
});
From Node.js Backend:
const cloudbase = require("@cloudbase/node-sdk");
const app = cloudbase.init({
env: "your-env-id"
});
const result = await app.callFunction({
name: "functionName",
data: { /* function parameters */ }
});
From HTTP API:
Use CloudBase HTTP API to invoke functions:
https://api.cloudbase.net/v1/{envId}/functions/{functionName}/invokehttp-api skill for detailsHTTP Access vs HTTP API:
https://api.cloudbase.net/v1/{envId}/functions/{functionName}/invoke) with authentication tokenCreating HTTP Access:
Primary Method: Use createFunctionHTTPAccess tool (see MCP tool documentation).
Alternative Method (Plan B): If tool unavailable, use callCloudApi with CreateCloudBaseGWAPI:
callCloudApi({
service: "tcb",
action: "CreateCloudBaseGWAPI",
params: {
EnableUnion: true,
Path: "/api/users",
ServiceId: "{envId}",
Type: 6,
Name: "functionName",
AuthSwitch: 2,
PathTransmission: 2,
EnableRegion: true,
Domain: "*" // Use "*" for default domain, or custom domain name
}
})
Key Parameters:
Type: 6 - Cloud Function type (required)AuthSwitch: 2 - No auth (1 = with auth)Domain: "*" - Default domain, or specify custom domainAccess URL: https://{envId}.{region}.app.tcloudbase.com/{path} or https://{domain}/{path}
Environment Variables:
Set via func.envVariables when creating/updating:
{
envVariables: {
"DATABASE_URL": "mysql://...",
"API_KEY": "secret-key"
}
}
⚠️ CRITICAL: Environment Variable Update Constraint
When updating environment variables for existing functions:
getFunctionList with action=detail to get the function's current configurationCorrect Update Pattern:
// 1. First, get current function details
const currentFunction = await getFunctionList({
action: "detail",
name: "functionName"
});
// 2. Merge existing envVariables with new ones
const mergedEnvVariables = {
...currentFunction.EnvVariables, // Existing variables
...newEnvVariables // New/updated variables
};
// 3. Update with merged variables
await updateFunctionConfig({
funcParam: {
name: "functionName",
envVariables: mergedEnvVariables
}
});
Why This Matters:
Timeout Configuration:
Set via func.timeout (in seconds):
Timer Triggers:
Configure via func.triggers:
timer (only supported type)"0 0 2 1 * * *" - 2:00 AM on 1st of every month"0 30 9 * * * *" - 9:30 AM every dayVPC Configuration:
For accessing VPC resources:
{
vpc: {
vpcId: "vpc-xxxxx",
subnetId: "subnet-xxxxx"
}
}
Function Management:
getFunctionList - List functions or get function detailscreateFunction - Create new cloud functionupdateFunctionCode - Update function code (runtime cannot change)updateFunctionConfig - Update function configuration (⚠️ when updating envVariables, must first query and merge with existing values to avoid overwriting)Logging:
getFunctionLogs - Get function log list (basic info)getFunctionLogDetail - Get detailed log content by RequestIdcallCloudApi (Plan B) - Use GetFunctionLogs and GetFunctionLogDetail actions if direct tools unavailableHTTP Access:
createFunctionHTTPAccess - Create HTTP access for functioncallCloudApi (Plan B) - Use CreateCloudBaseGWAPI action if direct tool unavailableTriggers:
manageFunctionTriggers - Create or delete function triggersFunction Console URLs:
https://tcb.cloud.tencent.com/dev?envId=${envId}#/scfhttps://tcb.cloud.tencent.com/dev?envId=${envId}#/scf/detail?id=${functionName}&NameSpace=${envId}Console Features:
exports.main = async (event, context) => {
try {
// Function logic
return {
code: 0,
message: "Success",
data: result
};
} catch (error) {
return {
code: -1,
message: error.message,
data: null
};
}
};
exports.main = async (event, context) => {
const apiKey = process.env.API_KEY;
const dbUrl = process.env.DATABASE_URL;
// Use environment variables
};
const cloudbase = require("@cloudbase/node-sdk");
const app = cloudbase.init({
env: process.env.ENV_ID
});
exports.main = async (event, context) => {
const db = app.database();
const result = await db.collection("users").get();
return result;
};
Nodejs18.15 for new projectscloudrun-development - For multi-language backend serviceshttp-api - For HTTP API invocation patternscloudbase-platform - For general CloudBase platform knowledge