Skip to main content

Quick Start

This tutorial will guide you through the complete process from environment setup to cloud function deployment using CloudBase CLI

Prerequisites

Activate CloudBase Service

Log in to Tencent Cloud CloudBase Console and ensure that you have activated the CloudBase service and created an available environment. If you are not familiar with how to create an environment, please refer to the Create Environment documentation.

Step 1: Login Authentication

Log in to your Tencent Cloud account to authorize CloudBase CLI to operate your resources

Note for International Users

Before logging in, you need to execute the command tcb config set isIntl true to switch to the international route, or set the environment variable TCB_IS_INTL=true.

Enter in the terminal:

tcb login

CloudBase CLI will automatically open the CloudBase console to obtain authorization. Click the agree to authorize button to complete login.

Tips

Step 2: Create CloudBase Project

Initialize Project

Use the tcb new command to create a project, supporting multiple languages such as Node.js, PHP, Java, etc.

⚠️ Note: Using the tcb new command requires CLI version 1.0+

Use the following command to create a project, creating a Node.js cloud function project named app:

tcb new app node-starter

Project File Structure:

.
├── .gitignore
├── functions # Cloud functions directory
│ └── node-app
│ └── index.js
└── cloudbaserc.json # Project configuration file

Write Cloud Functions

Directory Conventions:

  • Node/Python: Stored in the functions directory, with the function name as the folder name
    • Example: functions/node-app/index.js
    • Example: functions/node-app/index.py
  • Java Functions: Rename the jar file to the function name and place it in the functions directory
    • Example: functions/cloudbase.jar

Custom Function Directory:

Specify the function storage directory (path relative to the project root directory) through the functionRoot option in the configuration file.

Example Code:

Create a Node.js function functions/app/index.js:

"use strict";

exports.main = async (event, context) => {
console.log("Hello World");
console.log("Event:", event);
console.log("Context:", context);

return {
code: 0,
message: "success"
};
};

Project Configuration File

The project configuration file is the cloudbaserc.json file in the root directory. The default generated configuration is for Node.js functions. Other languages (PHP, Java, etc.) need to modify the corresponding handler (entry point) and runtime.

For detailed configuration instructions, please refer to Configuration File Documentation.

Configuration Example:

{
"envId": "your-env-id",
"functionRoot": "./functions",
"functions": [
{
"name": "node-app",
"timeout": 5,
"envVariables": {},
"runtime": "Nodejs16.13",
"memorySize": 128,
"handler": "index.main"
}
]
}

Step 3: Deploy Cloud Functions

Deploy Specific Function

In the project root directory (where cloudbaserc.json is located), run the following command to deploy the node-app function:

tcb fn deploy node-app

View Function List

After deployment is complete, use the following command to view the list of deployed functions:

tcb fn list