Skip to main content

Deploy Cloud Functions

Use the tcb fn deploy command to quickly deploy cloud functions to the CloudBase environment.

tcb fn deploy [options] [name]

Command Parameters

ParameterDescriptionRequired
-e, --envId <envId>Environment IdNo
--httpFnDeploy as HTTP cloud functionNo
--wsEnable WebSocket protocol when deploying HTTP cloud functionsNo
--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 mode: cos (default, recommended), zip (not recommended, limited to 1.5 MB), or image (container image)No
-h, --helpView command helpNo

Basic Usage

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

# Deploy a specific function
tcb fn deploy <functionName>

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

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.

Deploy HTTP Functions

# Using command line parameter
tcb fn deploy <functionName> --httpFn

# Or from current directory
cd my-web-function
tcb fn deploy --httpFn

Usage Examples

Standard Workspace Deployment

project/
├── cloudbaserc.json
└── functions/
└── hello/
├── index.js
└── package.json
tcb fn deploy hello

Custom Function Directory

// cloudbaserc.json
{
"envId": "your-env-id",
"functionRoot": "./functions",
"functions": [
{
"name": "api",
"dir": "./src/cloud-functions/api",
"runtime": "Nodejs18.15"
}
]
}
tcb fn deploy api

Command Line Override

tcb fn deploy hello --dir ./my-custom-path/hello

Deployment Path Resolution

During deployment, CLI resolves the function code path with the following priority:

1. --dir parameter (command line)      →  Highest priority
2. functions[].dir (config file) → Second priority
3. functionRoot + function name → Default path (functionRoot defaults to ./functions)
4. Current working directory → Fallback when no config file
ScenarioConfig File--dirfunctions.dirFinal Path
Standard deploy✅ existsfunctionRoot/functionName
Custom directory✅ exists✅ specifiedfunctions.dir path
Command line override✅ exists✅ specified---dir path
No config file-Current working directory
Note

When batch deploying (using --all or without specifying function name), the --dir parameter is ignored.

Advanced Options

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
Important

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

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

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

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.

Detailed Guide

For complete bootstrap script documentation (requirements, templates, runtime paths, troubleshooting), see Bootstrap Script.

Container Image Deployment

Cloud functions support container image deployment, suitable for scenarios requiring custom runtime environments, non-standard languages, or complex system library dependencies.

Use Cases

  • Custom runtime environment requirements
  • Using languages beyond Node.js/Python (e.g., Go, Rust)
  • Dependencies on specific system libraries or binaries
  • Pre-installing large dependency packages
  • Migrating existing containerized applications

Prerequisites

Before deploying cloud functions using container images, ensure the following preparations are completed:

  1. Authorize image pull: Grant the QcloudAccessForSCFRoleInPullImage policy to the cloud function role, one-time operation: Click to authorize
  2. Prepare image repository: Create a repository in Tencent Cloud Container Registry TCR; refer to Obtain access credentials and Push images
  3. Image requirements: Linux-based amd64 image, container must listen on port 9000

Deployment Workflow

Step 1: Build Image

In the function code directory (containing Dockerfile), build the Docker image:

cd /path/to/your-function

# Build image (must specify linux/amd64 platform)
docker build --platform linux/amd64 -t your-function:v1 .
Important

When building on ARM architecture machines (e.g., M1/M2 Mac), you must add the --platform linux/amd64 parameter, otherwise the image will not run in the cloud function environment.

Step 2: Push Image

Push the image to Tencent Cloud Container Registry (TCR):

# Tag image
docker tag [imageId] <registry-domain>/your-namespace/your-function:[tag]

# Push image
docker push <registry-domain>/your-namespace/your-function:[tag]

Step 3: Configure and Deploy

Configure imageConfig in cloudbaserc.json:

{
"envId": "your-env-id",
"functions": [
{
"name": "your-function",
"imageConfig": {
"imageType": "personal",
"imageUri": "<registry-domain>/your-namespace/your-function:v1"
}
}
]
}

Execute deployment:

tcb fn deploy <functionName> --deployMode image
Configuration Details

For complete image configuration options, see Image Configuration.

Configuration File

Cloud function configuration is managed through the cloudbaserc.json file, including function name, runtime, timeout, environment variables, etc.

Configuration Details

For complete configuration options and examples, see Cloud Function Configuration.

Notes

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.