Skip to main content

Deploy Cloud Functions

Command Description

Use the tcb fn deploy command to quickly deploy cloud functions to the CloudBase environment. Supports deploying both regular cloud functions and HTTP functions (Web functions).

Basic Usage

Method 1: Using Configuration File

In a project directory containing the cloudbaserc.json configuration file, execute the following commands:

# Deploy a specific cloud function
tcb fn deploy <functionName>

# Deploy all cloud functions in the configuration file
tcb fn deploy

# Deploy as HTTP function
tcb fn deploy <functionName> --httpFn

Method 2: Deploy from Current Directory

In a function code directory (containing package.json), deploy directly without a configuration file:

cd my-function
tcb fn deploy

CLI will automatically read the function name from package.json and deploy with default configuration. To deploy as HTTP function, add the --httpFn parameter:

tcb fn deploy --httpFn

Command Parameters

tcb fn deploy [options] [name]
ParameterDescriptionRequired
-e, --envId <envId>Environment IdNo
--httpFnDeploy as HTTP cloud function (HTTP trigger)No
--code-secret <codeSecret>Protect code with 36-character alphanumeric stringNo
--forceOverwrite existing function with same nameNo
--path <path>Auto-create HTTP access service pathNo
--dir <dir>Specify cloud function folder pathNo
--allDeploy all cloud functions in config fileNo
--deployMode <deployMode>Upload method: cos or zip, defaults to cos (zip limited to 1.5 MB)No
-h, --helpView command helpNo

Usage Examples

# Deploy the app function
tcb fn deploy app

# Deploy all functions
tcb fn deploy

# Deploy to specific environment
tcb fn deploy app -e your-env-id

Code Encryption

Use the --code-secret parameter to encrypt code. The key must be 36 alphanumeric characters:

tcb fn deploy app --code-secret 7sGLwMnhgEfKmkqg2dMjB6xWk2hCxsAgGR6w
Note

After enabling code encryption, you will not be able to view cloud function code and information in the Mini Program IDE or Tencent Cloud console.

Overwrite Existing Functions

If a function with the same name already exists in the cloud, CLI will prompt whether to overwrite. To force overwrite, use the --force parameter:

tcb fn deploy app --force

Specify function directory for deployment:

tcb fn deploy app --dir ./functions/app
Important

Using the --force parameter to overwrite a function will also overwrite its configuration and triggers.

Configuration File Example

{
"envId": "your-env-id",
"functionRoot": "./functions",
"functions": [
{
"name": "app",
"timeout": 5,
"runtime": "Nodejs18.15",
"installDependency": true,
"handler": "index.main"
},
{
"name": "webFunction",
"type": "HTTP",
"timeout": 60,
"runtime": "Nodejs18.15",
"memorySize": 256,
"envVariables": {
"NODE_ENV": "production"
}
}
]
}
Note

HTTP functions require type to be set to HTTP

Default Configuration

For Node.js cloud functions, CLI provides default configuration, allowing deployment without manual configuration:

{
"timeout": 5,
"runtime": "Nodejs18.15",
"installDependency": true,
"handler": "index.main",
"ignore": ["node_modules", "node_modules/**/*", ".git"]
}

Deployment Process

When executing the tcb fn deploy command, CLI automatically performs the following operations:

  1. Package and Upload: Package function code into a compressed file and upload
  2. Configuration Update: Update function configuration (timeout, memory, environment variables, network settings, etc.)
  3. Trigger Deployment: Deploy or update triggers based on configuration file

HTTP Functions

HTTP functions are cloud function types optimized for web service scenarios, supporting standard HTTP request-response patterns.

Deployment Methods

Method 1: Using Configuration File

Set type to HTTP in cloudbaserc.json, then execute:

tcb fn deploy webFunction

Method 2: Using Command Line Parameter

tcb fn deploy <functionName> --httpFn

Method 3: Deploy from Current Directory

In a function code directory (containing package.json), deploy directly without a configuration file:

cd my-web-function
tcb fn deploy --httpFn

CLI will automatically read the function name from package.json and deploy with default configuration.

Bootstrap Script

HTTP functions require a scf_bootstrap bootstrap script to start the Web Server. If not created, CLI will prompt to auto-generate one.

Requirements

RequirementDescription
File nameMust be named scf_bootstrap, the only name recognized
File permissionMust have executable permission (755 or 777)
Script formatFirst line must contain #!/bin/bash, file must end with LF
PathStartup command must use absolute path
Listen addressUse 0.0.0.0, do not use 127.0.0.1
Listen portWeb Server must listen on port 9000

Template Examples

#!/bin/bash
export PORT=9000
/var/lang/node18/bin/node index.js

Supported Runtimes

Startup commands must use absolute paths, otherwise invocation will fail.

RuntimeBootstrap Script Path
Node.js 20.19/var/lang/node20/bin/node
Node.js 18.15/var/lang/node18/bin/node
Node.js 16.13/var/lang/node16/bin/node
Node.js 14.18/var/lang/node14/bin/node
Node.js 12.16/var/lang/node12/bin/node
Node.js 10.15/var/lang/node10/bin/node
Python 3.10/var/lang/python310/bin/python3.10
Python 3.9/var/lang/python39/bin/python3.9
Python 3.7/var/lang/python37/bin/python3.7
PHP 8.0/var/lang/php80/bin/php
PHP 7.4/var/lang/php74/bin/php
Java 11/var/lang/java11/bin/java
Java 8/var/lang/java8/bin/java

Set Permissions

chmod 755 scf_bootstrap
Note

Files edited on Windows may use CRLF line endings and need to be converted to LF format, otherwise startup will fail.

Common Errors

If function invocation returns 405 error, it usually indicates scf_bootstrap cannot run properly. Check:

  • Is the startup command path correct
  • Does the file have executable permission
  • Are line endings in LF format

Project Structure

my-web-function/
├── scf_bootstrap # Bootstrap script (required)
├── package.json # Project config and dependencies
├── cloudbaserc.json # CLI config file (optional)
└── index.js # Function code

Function Invocation

CloudBase cloud functions support multiple invocation methods to meet different scenarios and platform requirements. You can choose the most suitable invocation method based on your needs. For details, see Cloud Function Invocation.

Notes

Upload Method

Default is COS upload. Use --deployMode zip for ZIP upload (limited to 1.5 MB).

Function Type Cannot Be Changed

Deployed function type (regular/HTTP function) cannot be changed. To change type, delete the original function and redeploy.

Environment Variable Update Rules
  • @cloudbase/cli 2.12.0 and above: Supports choosing incremental update or overwrite update for environment variables during deployment
  • @cloudbase/cli below 2.12.0: Environment variable configuration in cloudbaserc.json will completely overwrite the online configured environment variables

Important: If you are using a version below 2.12.0 and have manually configured environment variables in the console, ensure these configurations are also included in cloudbaserc.json, otherwise the original environment variables will be lost after deployment.