Skip to main content

Quick Start

Develop and deploy cloud functions with CloudBase CLI.

Standard workspace​

A cloud function project uses the following layout:

project/
├── functions/ # Cloud functions directory
│ └── <functionName>/ # Function directory
│ ├── index.js # Function entry file
│ └── package.json # Dependencies (optional)
└── cloudbaserc.json # Project configuration file
  • functions/: root directory of function code, set by functionRoot in cloudbaserc.json.
  • cloudbaserc.json: project configuration with the environment ID and function configuration.

For configuration fields, see Function Configuration.

Deploy the first function​

Install and log in​

# International accounts use tcb login --intl true
npm install -g @cloudbase/cli
tcb login

Initialize configuration​

# Pull one function configuration; a default is generated when it does not exist
tcb config pull fn hello-world

# Pull several function configurations at once
tcb config pull fn func1 func2 func3

Write the function code​

Create the function in functions/hello-world/index.js:

exports.main = async (event, context) => {
return {
message: 'Hello World!',
input: event
};
};

Deploy​

tcb fn deploy hello-world

After deployment, test the function in the CloudBase console.

Common workflows​

Create a function​

tcb config pull fn <functionName> # 1. Initialize configuration
# Write code under functions/<functionName>/
tcb fn deploy <functionName> # 2. Deploy the function

Update a function​

tcb config pull fn <functionName> # 1. Sync cloud configuration
tcb fn code download <functionName> # 2. Download code if it is not available locally
# Edit code under functions/<functionName>/
tcb fn code update <functionName> # 3. Update the code

Batch operations​

# Pull several function configurations
tcb config pull fn func1 func2 func3

# Pull every function in the configuration file
tcb config pull fn --all

# Skip interactive confirmation
tcb config pull fn --all --yes

Notes​

  • Run tcb config pull fn before other operations so local configuration matches the cloud.
  • Deploy from the configuration file to keep workflows repeatable and automatable.
  • Manage environment variables through the configuration file instead of editing them manually.

Next steps​