Skip to main content

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 MethodUse CaseCore Capabilities
Manager Node SDKNode.js backend servicesComplete environment management API, high code integration
CLI Command Line ToolLocal development and deploymentCommand line operations, rapid deployment
MCP ToolsAI-driven automationNatural language operations, intelligent management

Use Cases

Typical Scenarios

  1. 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
  2. Education and Training Platforms

    • Create independent experimental environments for each student or class
    • Automatically clean up environments after course completion
    • Achieve elastic resource management
  3. 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:

  1. Sufficient Account Balance: Creating CloudBase environments requires charges, please ensure your Tencent Cloud account has sufficient balance
  2. API Keys: Obtain Tencent Cloud API keys (SecretId and SecretKey), Get API Keys
  3. 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

OperationDescriptionSupported Methods
Create EnvironmentCreate a new CloudBase environment for tenantSDK
Query EnvironmentGet environment list and detailed informationSDK / CLI / MCP
Modify ConfigurationUpdate environment name, package, and other configurationsSDK / CLI
Destroy EnvironmentDelete unused environmentsSDK
Domain ManagementAdd/remove security domain whitelistSDK / CLI / MCP

Detailed documentation:

Database Management

OperationDescriptionSupported Methods
Create CollectionCreate document database collectionSDK / CLI / MCP
Create TableCreate MySQL database tableSDK / MCP
Data ImportBatch import initial dataSDK
Index ManagementCreate and manage database indexesSDK / MCP
Permission ConfigurationSet database security rulesSDK

Detailed documentation:

Cloud Function Deployment

OperationDescriptionSupported Methods
Create FunctionCreate a new cloud functionSDK / CLI / MCP
Update CodeUpdate function code and configurationSDK / CLI / MCP
Query FunctionGet function list and detailed informationSDK / CLI / MCP
Trigger ManagementConfigure timer triggers, etc.SDK / CLI
View LogsQuery function runtime logsSDK / CLI / MCP

Detailed documentation:

CloudRun Deployment

OperationDescriptionSupported Methods
Create ServiceCreate CloudRun container serviceSDK / CLI / MCP
Deploy VersionDeploy new version imageSDK / CLI / MCP
Configuration ManagementUpdate service configuration and environment variablesSDK / CLI / MCP
Query StatusGet service runtime statusSDK / CLI / MCP
View LogsQuery service logsSDK / CLI / MCP

Detailed documentation:

Cloud Storage Management

OperationDescriptionSupported Methods
Upload FileUpload local files to cloud storageSDK / CLI / MCP
Download FileDownload cloud storage files to localSDK / CLI / MCP
Delete FileDelete files in cloud storageSDK / CLI / MCP
Directory ManagementCreate and manage storage directoriesSDK / CLI / MCP
Permission SettingsConfigure file access permissionsSDK / CLI

Detailed documentation:

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:

  1. Use Manager Node SDK to regularly query environment information
  2. View monitoring dashboard through CloudBase Console