Multi-Tenant Environment Management Best Practices
Overview
For SaaS platforms, educational institutions, or enterprise service providers, it may be necessary to provide an independent CloudBase environment for each customer or tenant. CloudBase offers comprehensive programmatic management capabilities, allowing service providers to automatically create environments, configure resources, and deploy applications through code, implementing a multi-tenant architecture where "one tenant corresponds to one independent environment".
This document introduces how to use three local tools to complete the full lifecycle management of environments:
| Tool Method | Use Case | Core Capabilities |
|---|---|---|
| Manager Node SDK | Node.js backend services | Complete environment management API, high code integration |
| CLI Command Line Tool | Local development and deployment | Command line operations, rapid deployment |
| MCP Tools | AI-driven automation | Natural language operations, intelligent management |
Use Cases
Typical Scenarios
SaaS Platform Multi-Tenant Isolation
- Each enterprise customer has an independent CloudBase environment
- Data is completely isolated with no mutual interference
- Environments can be dynamically created and destroyed based on customer needs
Education and Training Platforms
- Create independent experimental environments for each student or class
- Automatically clean up environments after course completion
- Achieve elastic resource management
Project Management Platforms
- Create independent development environments for each project
- Release environment resources after project archival
- Support complete isolation between projects
Architecture Advantages
- Data Isolation: Each tenant has independent database, storage, and function resources
- Security and Reliability: Complete isolation between tenants, avoiding data leakage risks
- Flexible Scaling: Dynamically create and release environments based on business needs
- Unified Management: Service providers manage all tenant environments through a unified interface
- Cost Optimization: Create and destroy environments on demand, optimizing resource usage
Prerequisites
Before starting, please ensure:
- Sufficient Account Balance: Creating CloudBase environments requires charges, please ensure your Tencent Cloud account has sufficient balance
- API Keys: Obtain Tencent Cloud API keys (SecretId and SecretKey), Get API Keys
- Permission Configuration: Ensure API keys have CloudBase environment management permissions
Core Management Capabilities
Service providers can complete the following environment management operations through local tools:
Environment Management
| Operation | Description | Supported Methods |
|---|---|---|
| Create Environment | Create a new CloudBase environment for tenant | SDK |
| Query Environment | Get environment list and detailed information | SDK / CLI / MCP |
| Modify Configuration | Update environment name, package, and other configurations | SDK / CLI |
| Destroy Environment | Delete unused environments | SDK |
| Domain Management | Add/remove security domain whitelist | SDK / CLI / MCP |
Detailed documentation:
- Manager Node SDK - Environment Management API
- CLI - Environment Management
- MCP Tools - Environment Management
Database Management
| Operation | Description | Supported Methods |
|---|---|---|
| Create Collection | Create document database collection | SDK / CLI / MCP |
| Create Table | Create MySQL database table | SDK / MCP |
| Data Import | Batch import initial data | SDK |
| Index Management | Create and manage database indexes | SDK / MCP |
| Permission Configuration | Set database security rules | SDK |
Detailed documentation:
Cloud Function Deployment
| Operation | Description | Supported Methods |
|---|---|---|
| Create Function | Create a new cloud function | SDK / CLI / MCP |
| Update Code | Update function code and configuration | SDK / CLI / MCP |
| Query Function | Get function list and detailed information | SDK / CLI / MCP |
| Trigger Management | Configure timer triggers, etc. | SDK / CLI |
| View Logs | Query function runtime logs | SDK / CLI / MCP |
Detailed documentation:
- Manager Node SDK - Cloud Function Management API
- CLI - Cloud Function Management
- MCP Tools - Cloud Function Deployment
CloudRun Deployment
| Operation | Description | Supported Methods |
|---|---|---|
| Create Service | Create CloudRun container service | SDK / CLI / MCP |
| Deploy Version | Deploy new version image | SDK / CLI / MCP |
| Configuration Management | Update service configuration and environment variables | SDK / CLI / MCP |
| Query Status | Get service runtime status | SDK / CLI / MCP |
| View Logs | Query service logs | SDK / CLI / MCP |
Detailed documentation:
Cloud Storage Management
| Operation | Description | Supported Methods |
|---|---|---|
| Upload File | Upload local files to cloud storage | SDK / CLI / MCP |
| Download File | Download cloud storage files to local | SDK / CLI / MCP |
| Delete File | Delete files in cloud storage | SDK / CLI / MCP |
| Directory Management | Create and manage storage directories | SDK / CLI / MCP |
| Permission Settings | Configure file access permissions | SDK / CLI |
Detailed documentation:
- Manager Node SDK - Cloud Storage Management API
- CLI - Cloud Storage Management
- MCP Tools - File Management
Solution Selection
Choose the appropriate management method based on your tech stack and use case:
Manager Node SDK
Use Case: Node.js backend services, requiring deep integration of environment management capabilities
Advantages:
- Complete TypeScript type support
- High code integration, easy to maintain
- Rich API encapsulation
- Comprehensive error handling
Quick Start:
npm install @cloudbase/manager-node
import CloudBase from '@cloudbase/manager-node'
const manager = new CloudBase({
secretId: 'Your SecretId',
secretKey: 'Your SecretKey'
})
// Create environment
const result = await manager.env.createEnv({
alias: 'Tenant A Exclusive Environment'
})
Complete Documentation: Manager Node SDK Documentation
CLI Command Line Tool
Use Case: Local development and deployment, requiring quick execution of management operations
Advantages:
- Command line operations, simple and intuitive
- Rapid deployment and resource management
- Suitable for local development environments
- No code writing required
Quick Start:
# Install CLI
npm i -g @cloudbase/cli
# Login
tcb login
# View environment list
tcb env list
# Create database collection
tcb db list
# Deploy cloud function
tcb fn deploy
# Deploy CloudRun service
tcb cloudrun deploy
Complete Documentation: CloudBase CLI Documentation
MCP Tools
Use Case: AI-driven automated management, rapid prototyping
Advantages:
- Natural language operations, no coding required
- Quickly implement automation workflows
- Suitable for integration with AI assistants
- Lower development barriers
Quick Start:
After installing CloudBase AI Toolkit, interact with AI assistant through natural language:
User: Create a new environment for tenant A
AI: Creating environment...
User: Create a database collection named users for this environment
AI: Collection users created
User: Deploy cloud function login to this environment
AI: Deploying cloud function...
Complete Documentation: CloudBase MCP Tools Documentation
Complete Example: Multi-Tenant Management System
The following is a complete multi-tenant management system example, demonstrating how to use Manager Node SDK to manage tenant environments:
import CloudBase from '@cloudbase/manager-node'
class TenantManager {
constructor(secretId, secretKey) {
this.secretId = secretId
this.secretKey = secretKey
this.tenantEnvMap = new Map()
}
// Get manager instance
getManager(envId) {
return new CloudBase({
secretId: this.secretId,
secretKey: this.secretKey,
envId: envId
})
}
// Create environment for tenant
async createTenantEnv(tenantId, config) {
try {
const manager = new CloudBase({
secretId: this.secretId,
secretKey: this.secretKey
})
// Create environment
const result = await manager.env.createEnv({
alias: config.alias || `${tenantId}-env`,
payMode: 'postpay'
})
const envId = result.EnvId
this.tenantEnvMap.set(tenantId, envId)
console.log(`Tenant ${tenantId} environment created successfully: ${envId}`)
return envId
} catch (error) {
console.error(`Failed to create environment:`, error)
throw error
}
}
// Initialize tenant environment
async setupTenantEnv(tenantId, config) {
const envId = this.tenantEnvMap.get(tenantId)
if (!envId) throw new Error(`Tenant ${tenantId} not bound to environment`)
const manager = this.getManager(envId)
// 1. Configure security domains
if (config.domains) {
await manager.env.createEnvDomain(config.domains)
}
// 2. Create database collections
if (config.collections) {
for (const collection of config.collections) {
await manager.database.createCollection(collection)
}
}
// 3. Deploy cloud functions
if (config.functions) {
for (const func of config.functions) {
await manager.functions.createFunction({
functionName: func.name,
functionRootPath: func.path
})
}
}
// 4. Upload initial files
if (config.files) {
for (const file of config.files) {
await manager.storage.uploadFile({
localPath: file.localPath,
cloudPath: file.cloudPath
})
}
}
console.log(`Tenant ${tenantId} environment configuration completed`)
}
// Query tenant environment information
async getTenantEnvInfo(tenantId) {
const envId = this.tenantEnvMap.get(tenantId)
if (!envId) throw new Error(`Tenant ${tenantId} not bound to environment`)
const manager = this.getManager(envId)
const envInfo = await manager.env.getEnvInfo()
return {
tenantId,
envId,
envInfo: envInfo.EnvInfo
}
}
// Destroy tenant environment
async destroyTenantEnv(tenantId) {
const envId = this.tenantEnvMap.get(tenantId)
if (!envId) throw new Error(`Tenant ${tenantId} not bound to environment`)
const manager = this.getManager(envId)
// Destroy environment
await manager.env.destroyEnv()
this.tenantEnvMap.delete(tenantId)
console.log(`Tenant ${tenantId} environment destroyed`)
}
}
// Usage example
async function example() {
const tenantManager = new TenantManager(
'Your SecretId',
'Your SecretKey'
)
// 1. Create environment for tenant A
const envId = await tenantManager.createTenantEnv('tenant-a', {
alias: 'Tenant A Exclusive Environment'
})
// 2. Initialize environment
await tenantManager.setupTenantEnv('tenant-a', {
domains: ['tenant-a.example.com'],
collections: ['users', 'orders'],
functions: [
{ name: 'login', path: './functions/login' },
{ name: 'getUserInfo', path: './functions/getUserInfo' }
],
files: [
{ localPath: './assets/logo.png', cloudPath: '/assets/logo.png' }
]
})
// 3. Query environment information
const info = await tenantManager.getTenantEnvInfo('tenant-a')
console.log('Tenant environment information:', info)
}
FAQ
Q: Is environment creation through API supported?
Yes. Environments can be created through the following three methods:
- Manager Node SDK:
manager.env.createEnv() - MCP Tools: Create through AI assistant natural language
Q: How to destroy unused environments?
Environments can be destroyed through the following methods:
- Manager Node SDK:
manager.env.destroyEnv() - MCP Tools: Destroy through AI assistant natural language
Please ensure important data is backed up before destruction.
Q: What is the maximum number of environments that can be created per account?
The CloudBase account environment limit is 50 environments.
Q: How to monitor resource usage of multiple environments?
Monitoring can be done through the following methods:
- Use Manager Node SDK to regularly query environment information
- View monitoring dashboard through CloudBase Console