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 PostgreSQL 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
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 access a PostgreSQL database. The configuration process is as follows:
test-postgreSQL
folder as an example. .env
file in test-postgreSQL
and configure your Tencent Cloud SecretId
, SecretKey
, region, and AZ information in the following format in the file:# .env
TENCENT_SECRET_ID=xxx // `SecretId` of your account
TENCENT_SECRET_KEY=xxx // `SecretKey` of your account
# Region and AZ configuration
REGION=ap-guangzhou // Resource deployment region, which refers to the region where the function and static webpage are deployed
ZONE=ap-guangzhou-2 // Resource deployment AZ, which refers to the AZ where the database is deployed
- 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.
VPC
folder in test-postgreSQL
.serverless.yml
file in VPC
and enter the following content to configure the VPC and subnet:org: fullstack
app: fullstack-serverless-db
stage: dev
component: vpc # (required) name of the component. In that case, it's vpc.
name: serverlessVpc # (required) name of your vpc component instance.
inputs:
region: ${env:REGION}
zone: ${env:ZONE}
vpcName: serverless
subnetName: serverless
DB
folder in test-postgreSQL
.serverless.yml
file in DB
and enter the following content to create and configure a PostgreSQL database:org: fullstack
app: fullstack-serverless-db
stage: dev
component: postgresql
name: fullstackDB
inputs:
region: ${env:REGION}
zone: ${env:ZONE}
dBInstanceName: ${name}
vpcConfig:
vpcId: ${output:${stage}:${app}:serverlessVpc.vpcId}
subnetId: ${output:${stage}:${app}:serverlessVpc.subnetId}
extranetAccess: false
api
folder in test-postgreSQL
to store the business logic code and relevant dependencies.src
folder in the api
folder, enter it on the command line, and run the following command to install the PostgreSQL dependency package:npm install pg
index.js
file in the src
folder and enter the following sample code, so that you can use the Serverless DB SDK to create a connection pool and call the database through the function:'use strict';
const { Pool } = require('pg');
exports.main_handler = async (event, context, callback) => {
let pgPool = new Pool({
connectionString: process.env.PG_CONNECT_STRING,
});
await pgPool.query(`CREATE TABLE IF NOT EXISTS users (
ID serial NOT NULL,
NAME TEXT NOT NULL,
EMAIL CHAR(50) NOT NULL,
SITE CHAR(50) NOT NULL
);`);
const client = await pgPool.connect();
const { rows } = await client.query({
text: 'select * from users',
});
await client.end();
console.log('pgsql query result:',rows)
}
serverless.yml
file in api
and enter the following content to configure the Connection String
of Serverless DB in the environment variables:org: fullstack
app: fullstack-serverless-db
stage: dev
component: scf
name: fullstack-serverless-db
inputs:
name: ${app}
src:
src: ./src
exclude:
- .env
region: ${env:REGION}
runtime: Nodejs10.15
handler: index.main_handler
timeout: 30
vpcConfig:
vpcId: ${output:${stage}:${app}:serverlessVpc.vpcId}
subnetId: ${output:${stage}:${app}:serverlessVpc.subnetId}
environment:
variables:
PG_CONNECT_STRING: ${output:${stage}:${app}:fullstackDB.private.connectionString}
test-postgreSQL
directory on the command line:sls deploy --all
If the following result is returned, the deployment is successful:serverless ⚡ framework
serverlessVpc:
region: ap-guangzhou
zone: ap-guangzhou-2
vpcId: vpc-0ncak84t
vpcName: serverless
subnetId: subnet-gi085his
subnetName: serverless
fullstackDB:
region: ap-guangzhou
zone: ap-guangzhou-2
vpcConfig:
subnetId: subnet-gi085his
vpcId: vpc-0ncak84t
dBInstanceName: fullstackDB
dBInstanceId: postgres-0y2x3fd3
private:
connectionString: postgresql://tencentdb_0y2x3fd3:GD0U1(q~g7%3D6ySI@10.0.0.10:5432/tencentdb_0y2x3fd3
host: 10.0.0.10
port: 5432
user: tencentdb_0y2x3fd3
password: GD0U1(q~g7=6ySI
dbname: tencentdb_0y2x3fd3
fullstack-serverless-db:
FunctionName: fullstack-serverless-db
Description:
Namespace: default
Runtime: Nodejs10.15
Handler: index.main_handler
MemorySize: 128
25s › fullstack-serverless-db › Success
You can also use Serverless Dashboard to monitor the deployed project in real time with ease.
Run the following command in the test-postgreSQL
directory to remove the project:
sls remove --all
If the following result is returned, the removal is successful:
serverless ⚡ framework
38s › tencent-fullstack › Success
Was this page helpful?