You can use the Serverless Framework component to create, deploy, and manage a Serverless DB instance easily and connect to and access the database through the SCF SDK. Based on the serverless services in the cloud, you can conveniently develop and rapidly deploy your business with "zero" configuration so as to implement it more easily.
Currently, Serverless Framework supports connecting to and deploying two types of databases: PostgreSQL and NoSQL. This document describes how to use an SCF function to connect to a NoSQL database.
You have installed Serverless Framework on at least the following versions; otherwise, please install it as instructed in Installing Serverless Framework.
Framework Core: 1.67.3
Plugin: 3.6.6
SDK: 2.3.0
Components: 2.30.1
SLS_QcsRole
under your account has been granted the QcloudTCBFullAccess
permission; otherwise, please go to the CAM Console to configure it.This document uses a function in the Node.js programming language as an example to describes how to use the Serverless Framework component to write and create a function and use it to create and access a NoSQL database. The configuration process is as follows:
test-NoSQL
folder as an example.serverless login
.env
file in test-NoSQL
and configure the corresponding Tencent Cloud SecretId
and SecretKey
as follows:# .env
TENCENT_SECRET_ID=xxx // `SecretId` of your account
TENCENT_SECRET_KEY=xxx // `SecretKey` of your account
- If you don't have a Tencent Cloud account yet, please sign up first.
- If you already have a Tencent Cloud account, please make sure that your account has been granted the
AdministratorAccess
permission. In addition, you can getSecretId
andSecretKey
in API Key Management.
DB
folder in test-NoSQL
.serverless.yml
file in the DB
folder and enter the following content to use the Serverless Framework component to configure the TCB environment:# serverless.yml
component: mongodb
name: mongoDBDemoMongo
org: anycodes
app: mongoDBAPP
stage: dev
inputs:
name: Mydemo
function
folder in test-NoSQL
to store the business logic code and relevant dependencies.src
folder in the function
folder, enter it on the command line, and run the following command to install the TCB dependency package:npm install --save tcb-admin-node@latest
index.js
file in the src
folder and enter the following sample code, so that you can use the Serverless TCB SDK to call the TCB environment through the function and call the NoSQL database in the environment:const tcb = require('tcb-admin-node')
const app = tcb.init({
secretId: process.env.SecretId,
secretKey: process.env.SecretKey,
env: process.env.MongoId,
serviceUrl: 'https://tcb-admin.tencentcloudapi.com/admin'
})
const db = app.database()
const _ = db.command
exports.main = async (event, context) => {
await db.createCollection('serverless')
const username = 'serverless'
const collection = db.collection('serverless')
if (username) {
await collection.add({username: username})
}
const userList = await collection.get()
return userList
}
serverless.yml
file and enter your SecretId and SecretKey in the environment variables.If you use the following configuration, a TCB environment will be created free of charge. If you already have a free TCB environment, please enter its ID in
MongoId
; otherwise, an error will be reported.
component: scf
name: mongoDBDemoSCF
org: anycodes
app: mongoDBAPP
stage: dev
inputs:
name: MongoDBDemo
src: ./src
runtime: Nodejs8.9
region: ap-guangzhou
handler: index.main
environment:
variables:
SecretId: enter your `SecretId`
SecretKey: enter your `SecretKey`
MongoId: ${output:${stage}:${app}:mongoDBDemoMongo.EnvId}
test-NoSQL
on the command line:sls deploy --all
If the following result is returned, the deployment is successful:serverless ⚡ framework
mongoDBDemoMongo:
Region: ap-guangzhou
Name: Mydemo
EnvID: Mydemo-dyxfxv
FreeQuota: basic
mongoDBDemoSCF:
FunctionName: MongoDBDemo
Description:
Namespace: default
Runtime: Nodejs8.9
Handler: index.main
MemorySize: 128
25s › tcbdemo › Success
You can also use Serverless Dashboard to monitor the deployed project in real time with ease.
Run the following command in the test-NoSQL
directory to remove the project:
sls remove --all
If the following result is returned, the removal is successful:
serverless ⚡ framework
4s › test-NoSQL › Success
Was this page helpful?