Skip to main content

Deploying SCF

Using the tcb fn deploy command can quickly deploy SCF to the TCB environment.

tcb fn deploy [options] [name]

Command Parameters

ParameterDescriptionRequired
-e, --env-id <envId>Environment IdNo
--httpFndeploy as HTTP SCFNo
--wsenable WebSocket protocol when deploying HTTP SCFNo
--code-secret <codeSecret>Passing this parameter will protect the code, in the format of 36 uppercase and lowercase letters and numbersNo
--forceOverwrite the function with the same name if it existsNo
--path <path>automatically creates the HTTP service access pathNo
--dir <dir>specify the folder path of the SCFNo
--alldeploy all SCF in the configuration fileNo
--deployMode <deployMode>Deployment mode: cos (recommended by default), zip (not recommended, limited to 1.5 MB), image (container image)No
--yesSkip interactive confirmation and directly execute deploymentNo
-h, --helpDisplay command help informationNo

Basic Usage

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
ScenarioConfiguration File--dirfunctions.dirResolved Path
Standard Deployment✅ ExistsfunctionRoot/function name
Custom Directory✅ Exists✅ Specifiedfunctions.dir path
Command Line Override✅ Exists✅ Specified---dir path
No Configuration File-Current working directory
Caution

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
Important

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
Important

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
Automated Deployment Recommendations

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.

Detailed Description

For the complete instructions on the startup script (Basic Requirements, Example Templates, Runtime Paths, and Troubleshooting Common Errors), see Startup Script.

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:

  1. Authorize Image Pulling: Grant the policy QcloudAccessForSCFRoleInPullImage to the SCF role. One-time operation: Click to authorize
  2. Prepare the image repository: Create a repository in Tencent Cloud Container Image Service TCR; refer to obtain access credentials and push images
  3. Image Requirements: Based on a Linux amd64 image, listens on the 9000 port within the container
  4. Create a log role (for log delivery):
    • Create a role in the CAM Role Console
    • Select Tencent Cloud product service, specify scf and cls as 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 .
Important

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"
}
]
}
Log Configuration

SCF deployed via container images requires manual configuration of log delivery:

  1. In the Log Service Console, create a logset and a log topic, and obtain the clsLogsetId and clsTopicId
  2. Fill in the role field with the log role name created in the preconditions.

Execute deployment:

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

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.

Configuration Details

Complete description of configuration items and examples, see SCF Configuration File.

Precautions

Function type cannot be changed

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.

Environment Variables Update Rules
  • @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.json will 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.