Cloud Function Management
Cloud functions are one of the core features of the cloud development platform. Effective management of cloud functions can improve the performance, reliability, and security of applications. This document will guide you on how to comprehensively manage the lifecycle of cloud functions, including operations such as creating, configuring, deploying, monitoring, and deleting.
Contents
- Management Methods
- Create Cloud Function
- Development and Deployment
- Function Configuration
- Function Monitoring
- Delete Cloud Function
- Management with cli Tools
- Frequently Asked Questions
Management Methods
You can manage cloud functions in the following two ways:
- Console Management: Use the CloudBase platform Cloud Function Management Page for visual operations
- Command-line Management: Use the CloudBase CLI tool to perform command-line operations
Different management methods are suitable for different scenarios. Console Management is ideal for quick operations and visual configuration, while Command-line Management is better suited for automated deployment and batch operations.
Create Cloud Function
Create via Console
- Log in to the CloudBase platform and access the Cloud Function Management Page
- Click the 'Create Cloud Function' button
- Fill in the function name, runtime environment, function description, and other information
- Select a function template or start writing code from scratch
- Click 'OK' to complete the creation
Create via CLI
Using the CloudBase CLI tool, you can create and deploy cloud functions with the following command:
tcb functions:deploy [functionName] --code ./function-code
Development and Deployment
Online Editing and Deployment
You can directly edit cloud function code in the CloudBase console:
- Select the target function on the Cloud Function Management Page
- Modify the code in the code editor
- Click the 'Save and Deploy' button to complete the deployment
Deploy via Code Package Upload
For more complex projects, you can package and upload the code:
- Package the function code and dependencies into a ZIP file
- On the cloud function details page, select 'Upload ZIP Package'
- Select and upload the local ZIP file
- Click 'Save and Deploy' to complete the deployment
The root directory of the ZIP package should contain the entry file, such as index.js
in the Node.js environment.
Update Code via CLI
Update cloud function code using the CLI tool:
tcb functions:code:update <functionName> --code ./function-code
Function Configuration
On the Cloud Function Management Page, select the target function and click the 'Configuration' tab to adjust the following configuration items:
Basic Configuration
Item | Description | Default Value | Allowed Values |
---|---|---|---|
Memory Configuration | Maximum memory limit for cloud function runtime | 256MB | 128MB - 2048MB |
Timeout | Maximum function execution time; function will be forcibly terminated upon timeout | 20s | 1s - 900s |
Environment Variables | Environment variables defined as key/value pairs | None | Custom |
Memory configuration affects the function's performance and billing. Please set it appropriately based on actual requirements.
Network Configuration
Public Network Access
Cloud Functions have public network access enabled by default. You can choose to disable it based on security requirements. Once disabled, the function will not be able to access public network resources.
Intranet Access (VPC Configuration)
You can configure Cloud Functions to access the VPC private network:
- Not configured VPC: Function instances run in an isolated network environment with public network access capability.
- Configured VPC: Function instances run in the specified VPC network and can access resources within the VPC.
- After configuring VPC, if public network access is required, please ensure that the VPC network is configured with a public network gateway or a NAT gateway
- For more information on VPC network configuration, please refer to the VPC Private Network Documentation
- For detailed information on Cloud Function network configuration, please refer to Network Configuration Details
Fixed Egress IP
After enabling this feature, Cloud Functions will be assigned a fixed public egress IP. This IP will be shared with other functions that have this feature enabled in the same namespace.
The fixed egress IP feature will take effect only when public network access is enabled for Cloud Functions.
Update Configuration via CLI
Update cloud function configuration using the CLI tool:
tcb functions:config:update [functionName] --memory 512 --timeout 60
Function Monitoring
View Runtime Logs
You can view the runtime logs of cloud functions through the following methods:
- On the Cloud Function details page, select the Logs tab
- Set the time range and log level
- View function execution logs
View logs via CLI:
tcb functions:log <functionName> --limit 20
Monitoring Metrics
On the Cloud Function details page, in the Monitoring tab, you can view the following key metrics:
- Invocation count
- Error count
- Average execution duration
- Memory usage
Deleting Cloud Function
Delete via Console
- Locate the target function on the Cloud Function Management Page
- Click "More" → "Delete"
- In the confirmation dialog box, enter the function name to confirm deletion.
Deleting a cloud function is irreversible. After deletion, the function will be inaccessible. Please proceed with caution.
Delete via CLI
Use the CLI tool to delete cloud function:
tcb functions:delete [functionName]
Managing Cloud Functions with CLI Tools
The CloudBase CLI tool provides comprehensive cloud function management commands, enabling automated deployment and management:
# List cloud functions
tcb functions:list [options]
# Download Cloud Function Code
tcb functions:download [options] <functionName> [dest]
# Deploy Cloud Function
tcb functions:deploy [options] [functionName]
# Delete Cloud Function
tcb functions:delete [options] [functionName]
# Get Cloud Function Information
tcb functions:detail [options] [functionName]
# Update Cloud Function Code
tcb functions:code:update [options] <functionName>
# Update Cloud Function Configuration
tcb functions:config:update [options] [functionName]
# Copy Cloud Function
tcb functions:copy [options] <functionName> <newFunctionName>
# Print Cloud Function Logs
tcb functions:log [options] <functionName>
# Create Cloud Function Trigger
tcb functions:trigger:create [options] [functionName]
# Delete Cloud Function Trigger
tcb functions:trigger:delete [options] [functionName] [triggerName]
# Trigger Cloud-Deployed Function
tcb functions:invoke [options] [functionName]
# Run Cloud Functions Locally (Currently only supports Node)
tcb functions:run [options]
For more detailed information and advanced usage, please refer to the CloudBase CLI tool documentation.
Frequently Asked Questions
What to do if Cloud Function deployment fails?
- Check the code for syntax errors
- Confirm whether the dependency packages are compatible with the cloud function environment
- Check the deployment logs to identify the specific error cause
- Try to reduce the deployment package size, as cloud function deployment packages have size limits
How to resolve cloud function execution timeout?
- Increase the function timeout configuration
- Optimize code execution efficiency
- For long-running tasks, consider splitting them into multiple functions or using asynchronous execution.
How to handle cloud function cold start issues?
- Keep function code concise and reduce unnecessary dependencies
- Use provisioned concurrency instances
- Periodically warm up functions to keep them active
How do cloud functions access databases?
Cloud functions can directly access the CloudBase database via the SDK without additional authentication configuration. Sample code:
const tcb = require('@cloudbase/node-sdk');
const app = tcb.init();
const db = app.database();
exports.main = async (event, context) => {
const collection = db.collection('users');
const result = await collection.get();
return result;
};
For more cloud function related questions, please refer to Cloud Function FAQ.