Deploying SCF
Using the tcb fn deploy command can quickly deploy SCF to the TCB environment.
tcb fn deploy [options] [name]
Command Parameters
| Parameter | Description | Required |
|---|---|---|
-e, --env-id <envId> | Environment Id | No |
--httpFn | deploy as HTTP SCF | No |
--ws | enable WebSocket protocol when deploying HTTP SCF | No |
--code-secret <codeSecret> | Passing this parameter will protect the code, in the format of 36 uppercase and lowercase letters and numbers | No |
--force | Overwrite the function with the same name if it exists | No |
--path <path> | automatically creates the HTTP service access path | No |
--dir <dir> | specify the folder path of the SCF | No |
--all | deploy all SCF in the configuration file | No |
--deployMode <deployMode> | Deployment mode: cos (recommended by default), zip (not recommended, limited to 1.5 MB), image (container image) | No |
--yes | Skip interactive confirmation and directly execute deployment | No |
-h, --help | Display command help information | No |
Basic Usage
Method 1: Using a Configuration File (Recommended)
Execute in the project directory containing the cloudbaserc.json configuration file:
# Deploying Specified Functions
tcb fn deploy <functionName>
# Deploying All Functions in the Configuration File
tcb fn deploy --all
Method 2: Deploying from the Current Directory
In the function code directory (containing package.json), you can deploy directly without a configuration file:
cd my-function
tcb fn deploy
The CLI automatically reads the function name from package.json and deploys it using the default configuration.
Deploying HTTP Functions
Add the --httpFn parameter to deploy as an HTTP function:
tcb fn deploy <functionName> --httpFn
Usage Example
Standard Workspace Deployment
project/
├── cloudbaserc.json
└── functions/
└── hello/
├── index.js
└── package.json
tcb fn deploy hello
Custom Functions Directory
// cloudbaserc.json
{
"envId": "your-env-id",
"functionRoot": "./functions",
"functions": [
{
"name": "api",
"dir": "./src/cloud-functions/api",
"runtime": "Nodejs18.15"
}
]
}
tcb fn deploy api
Specifying Directories via Command Line
tcb fn deploy hello --dir ./my-custom-path/hello
Deployment Path Parsing
During deployment, the CLI parses the function code path in the following order of priority:
1. --dir parameter (specified via command line) → Highest priority
2. functions[].dir (configuration file) → Secondary priority
3. functionRoot + function name → Default path (functionRoot defaults to ./functions)
4. Current working directory → Fallback when no configuration file is present
| Scenario | Configuration File | --dir | functions.dir | Resolved Path |
|---|---|---|---|---|
| Standard Deployment | ✅ Exists | ❌ | ❌ | functionRoot/function name |
| Custom Directory | ✅ Exists | ❌ | ✅ Specified | functions.dir path |
| Command Line Override | ✅ Exists | ✅ Specified | - | --dir path |
| No Configuration File | ❌ | ❌ | - | Current working directory |
During batch deployment (using --all or not specifying a function name), the --dir parameter is invalid and will be ignored.
Advanced Options
Code Encryption
By using the --code-secret parameter to encrypt the code, the key must be composed of 36 uppercase and lowercase letters and numbers:
tcb fn deploy app --code-secret 7sGLwMnhgEfKmkqg2dMjB6xWk2hCxsAgGR6w
After enabling code encryption, you will not be able to view the code and information of SCF in the Mini Program IDE or Tencent Cloud console.
Overwrite the function with the same name
If a function with the same name already exists in the cloud, the CLI will prompt whether to overwrite it. To force overwrite, you can use the --force parameter:
tcb fn deploy app --force
When using the --force parameter to overwrite a function, both the function's configuration and triggers will be overwritten.
Skip Interactive Confirmation
In automated scripts or CI/CD workflows, you can use the --yes parameter to skip all interactive confirmation:
# Skip Confirmation During Deployment
tcb fn deploy app --yes
# Skip Confirmation for Batch Deployment
tcb fn deploy --all --yes
# Force Overwrite and Skip Confirmation
tcb fn deploy app --force --yes
In CI/CD workflows, it is recommended to always add the --yes parameter to avoid script blocking while waiting for user input.
HTTP Functions
HTTP functions are a type of SCF specifically optimized for Web service scenarios, supporting the standard HTTP request-response model.
Deployment Method
Method 1: Using a Configuration File
In the cloudbaserc.json file, set type to HTTP and then execute:
tcb fn deploy webFunction
Method 2: Using Command-Line Parameters
tcb fn deploy <functionName> --httpFn
Method 3: Deploying from the Current Directory
cd my-web-function
tcb fn deploy --httpFn
Startup Script
HTTP functions require the scf_bootstrap startup script to start up the Web Server. If not created, the CLI prompts to auto-generate it.
For the complete instructions on the startup script (Basic Requirements, Example Templates, Runtime Paths, and Troubleshooting Common Errors), see Startup Script.
Related Guides
Image Deployment
SCF supports deployment via container images, which is suitable for scenarios requiring custom runtime environments, non-standard languages, or complex system library dependencies.
Applicable Scenarios
- Need to customize the runtime environment
- Use languages other than Node.js/Python (e.g., Go, Rust)
- Depend on specific system libraries or binary files
- Require pre-installation of numerous dependency packages
- Migration of existing containerized applications
Prerequisites
Before deploying SCF using container images, ensure that the following prerequisites are completed:
- Authorize Image Pulling: Grant the policy
QcloudAccessForSCFRoleInPullImageto the SCF role. One-time operation: Click to authorize - Prepare the image repository: Create a repository in Tencent Cloud Container Image Service TCR; refer to obtain access credentials and push images
- Image Requirements: Based on a Linux
amd64image, listens on the 9000 port within the container - Create a log role (for log delivery):
- Create a role in the CAM Role Console
- Select Tencent Cloud product service, specify
scfandclsas the role carrier - Associate the policy
QcloudCLSFullAccess - Record the role name (e.g.,
SCF_CLSFullAccess), to be used in subsequent configuration files
Deployment Process
Step 1: Build the image
In the function code directory (containing Dockerfile), build the Docker image:
cd /path/to/your-function
# Build the image (must specify the linux/amd64 platform)
docker build --platform linux/amd64 -t your-function:v1 .
When building on ARM-based machines (e.g., M1/M2 Mac), you must add the --platform linux/amd64 parameter; otherwise, the image cannot run in the SCF environment.
Step 2: Push the image
Push the image to Tencent Container Registry (TCR):
# Tag an image.
docker tag [imageId] <registry>/your-namespace/your-function:[tag]
# Push the image
docker push <registry>/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>/your-namespace/your-function:v1"
},
"role": "SCF_CLSFullAccess",
"clsLogsetId": "your-logset-id",
"clsTopicId": "your-topic-id"
}
]
}
SCF deployed via container images requires manual configuration of log delivery:
- In the Log Service Console, create a logset and a log topic, and obtain the
clsLogsetIdandclsTopicId - Fill in the
rolefield with the log role name created in the preconditions.
Execute deployment:
tcb fn deploy <functionName> --deployMode image
Complete description of image configuration items, see Image Configuration.
Configuration File
The configuration of SCF is managed through the cloudbaserc.json file, including function name, runtime, timeout, environment variables, etc.
Complete description of configuration items and examples, see SCF Configuration File.
Precautions
The function type (regular function/HTTP function) of a deployed function cannot be changed. To change the type, delete the original function and redeploy it.
- @cloudbase/cli version 2.12.0 and above: During deployment, supports selecting incremental update or overwrite update for environment variables.
- @cloudbase/cli versions below 2.12.0: The environment variable configurations in
cloudbaserc.jsonwill completely overwrite the environment variables configured online.
Important: If you are using a version below 2.12.0 and have manually configured environment variables in the console, please ensure these configurations are also included in cloudbaserc.json, otherwise the original environment variables will be lost after deployment.