Connecting to a YDB database from a Yandex Cloud Functions function in Node.js
In this scenario, you'll create a function with an application in Node.js, which runs a simple query against a YDB database. You deploy the application using Bash scripts and use the tcs
command to compile.
A function with an associated service account is authorized in YDB via the metadata service.
The application creates a YDB database connection driver, a session, and a transaction, and runs a query using the ydb
library. This library is installed as a dependency when creating a function version. The DB connection parameters are passed to the application via environment variables.
Before you start
Before working, you need to register in Yandex Cloud and create a billing account:
- Go to the management console. Then log in to Yandex Cloud or sign up if don't already have an account.
- On the billing page, make sure you linked a billing account, and it has the
ACTIVE
orTRIAL_ACTIVE
status. If you don't have a billing account, create one.
If you have an active billing account, you can create or select a folder to run your VM in from the Yandex Cloud page.
Learn more about clouds and folders.
Required paid resources
The infrastructure support cost for this scenario includes:
- A fee for using the function (see Yandex Cloud Functions pricing.
- A fee for querying the database (see Yandex Managed Service for YDB pricing).
Prepare the environment
- Clone the examples repository using Git.
- Install and initialize the following software programs:
- The Yandex Cloud command line interface.
- jq.
- Node.js.
npm
andnpx
.
- Install dependencies by running the command below:
npm install
Create a service account
-
In the management console, select the folder where you want to create a service account.
-
Go to the Service accounts tab.
-
Click Create service account.
-
Enter a name for the service account, such as
sa-function
. Naming requirements:- The length can be from 3 to 63 characters.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter. The last character can't be a hyphen.
-
Click Add role and choose the
editor
role. -
Click Create.
Create a YDB database
-
In the management console, select the folder where you want to create a database.
-
In the list of services, select Managed Service for YDB.
-
Click Create database.
-
Enter a name for the database. Naming requirements:
- The length can be from 3 to 63 characters.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter. The last character can't be a hyphen.
-
Under Database type, select the Serverless option.
-
Click Create database.
Wait until the database starts. When a database is being created, it has the
Provisioning
status. When it's ready for use, the status changes toRunning
. -
Click on the name of the created database.
-
Save the values of the following fields:
- Endpoint and Database from the YDB endpoint section.
- Endpoint from the Document API endpoint section.
You'll need them in the next step.
Create a table in the database
- In the management console, select the folder containing the database.
- In the list of services, select Managed Service for YDB.
- Select the database.
- Go to the Navigation tab.
- In the top right-hand corner, click Create. Then, in the resulting menu, click Table.
- In the Name field, enter
series
. Fill in the remaining fields however you'd like. - Click Create table.
Create a function
- Edit the
main.env
file:FUNCTION_NAME
: Function name.folder_id
: ID of the folder.SERVICE_ACCOUNT_ID
: Service account ID.DOCUMENT_API_ENDPOINT
: Value of the Endpoint field under Document API endpoint.DATABASENAME
: Value of the Database field under YDB endpoint.ENTRYPOINT
: String in the following format://Endpoint field value under YDB endpoint>. For example, if the protocol is grpcs
and the endpoint isydb.serverless.yandexcloud.net:2135
, entergrpcs://ydb.serverless.yandexcloud.net:2135
.YDB_SDK_LOGLEVEL
: Logging level. Legal values:fatal
,error
,warn
,info
,debug
, andtrace
.
- Create a function:
./first-setup-func.sh
- Create the function version:
./deploy.sh
Test the function
-
In the management console, select the folder containing the function.
-
In the list of services, select Cloud Functions.
-
Select a function.
-
Go to the Browse tab.
-
Under General information, click on the call link.
-
Once a successful connection to the database is established, the page will display a description of the
series
table fields in JSON format. For example:{ "driver": true, "tableDef": { "info": "Describe table series", "columns": [ { "name": "series_id", "type": 4 }, { "name": "title", "type": 4608 }, { "name": "series_info", "type": 4608 }, { "name": "release_date", "type": 48 } ] } }