Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
© 2022 Yandex.Cloud LLC
Yandex Managed Service for YDB
  • Getting started
  • Step-by-step instructions
    • Overview
    • Database management via the console Yandex Cloud
    • Database management using the YC CLI
    • Managing tables and directories
    • Reading and writing data
    • Monitoring DB status
  • Practical guidelines
    • Deploying a web application
    • Developing a Slack bot
    • Connecting to YDB from a Python function in Yandex Cloud Functions
    • Connecting to a YDB database from a Yandex Cloud Functions function in Node.js
    • Converting a video to a GIF in Python
    • Developing a skill for Alice and a website with authorization
  • Concepts
    • Overview
    • Terms and definitions
    • Connecting to and authenticating with a database using IAM
    • Serverless and Dedicated operation modes
    • DynamoDB tables
    • Quotas and limits
  • Access management
  • Pricing policy
    • Overview
    • Serverless mode
      • Pricing policy for serverless mode
      • Request cost for YQL
      • Request cost for the Document API
      • Request cost for special APIs
    • Dedicated mode
  • Amazon DynamoDB-compatible Document API
    • Tools
      • Setting up AWS tools
      • Working with data through the HTTP interface
      • Working with the AWS CLI
        • Overview
        • Creating a table
        • Adding data to a table
        • Reading data from a table
        • Updating data
        • Data selections
        • Deleting created resources
      • Working with the AWS SDK
        • Overview
        • Creating a table
        • Uploading data to a table
        • Managing records in a table
          • Creating a record
          • Reading a record
          • Updating a record
          • Deleting a record
        • Searching and extracting data
        • Deleting a table
    • Document API reference
      • All methods
      • Actions
        • BatchGetItem
        • BatchWriteItem
        • CreateTable
        • DeleteItem
        • DeleteTable
        • DescribeTable
        • DescribeTimeToLive
        • GetItem
        • ListTables
        • PutItem
        • Query
        • Scan
        • TransactGetItems
        • TransactWriteItems
        • UpdateItem
        • UpdateTimeToLive
    • Common errors when working with the Document API
  • API Reference Yandex Cloud for Managed Service for YDB
    • Overview
    • Authentication in the API
    • gRPC
      • Overview
      • BackupService
      • DatabaseService
      • LocationService
      • ResourcePresetService
      • StorageTypeService
      • OperationService
    • REST
      • Overview
      • Backup
        • Overview
        • delete
        • get
        • list
        • listPaths
      • Database
        • Overview
        • backup
        • create
        • delete
        • get
        • list
        • move
        • restore
        • start
        • stop
        • update
      • Location
        • Overview
        • get
        • list
      • ResourcePreset
        • Overview
        • get
        • list
      • StorageType
        • Overview
        • get
        • list
  • Questions and answers
  1. Practical guidelines
  2. Connecting to a YDB database from a Yandex Cloud Functions function in Node.js

Connecting to a YDB database from a Yandex Cloud Functions function in Node.js

Written by
Gayrat Vlasov
  • Before you start
    • Required paid resources
  • Prepare the environment
  • Create a service account
  • Create a YDB database
  • Create a table in the database
  • Create a function
  • Test the function

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:

  1. Go to the management console. Then log in to Yandex Cloud or sign up if don't already have an account.
  2. On the billing page, make sure you linked a billing account, and it has the ACTIVE or TRIAL_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

  1. Clone the examples repository using Git.
  2. Install and initialize the following software programs:
    • The Yandex Cloud command line interface.
    • jq.
    • Node.js.
    • npm and npx.
  3. Install dependencies by running the command below:
    npm install
    

Create a service account

Management console
  1. In the management console, select the folder where you want to create a service account.

  2. Go to the Service accounts tab.

  3. Click Create service account.

  4. 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.
  5. Click Add role and choose the editor role.

  6. Click Create.

Create a YDB database

Management console
  1. In the management console, select the folder where you want to create a database.

  2. In the list of services, select Managed Service for YDB.

  3. Click Create database.

  4. 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.
  5. Under Database type, select the Serverless option.

  6. 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 to Running.

  7. Click on the name of the created database.

  8. 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

Management console
  1. In the management console, select the folder containing the database.
  2. In the list of services, select Managed Service for YDB.
  3. Select the database.
  4. Go to the Navigation tab.
  5. In the top right-hand corner, click Create. Then, in the resulting menu, click Table.
  6. In the Name field, enter series. Fill in the remaining fields however you'd like.
  7. Click Create table.

Create a function

  1. 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 is ydb.serverless.yandexcloud.net:2135, enter grpcs://ydb.serverless.yandexcloud.net:2135.
    • YDB_SDK_LOGLEVEL: Logging level. Legal values: fatal, error, warn, info, debug, and trace.
  2. Create a function:
    ./first-setup-func.sh
    
  3. Create the function version:
    ./deploy.sh
    

Test the function

Management console
  1. In the management console, select the folder containing the function.

  2. In the list of services, select Cloud Functions.

  3. Select a function.

  4. Go to the Browse tab.

  5. Under General information, click on the call link.

  6. 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
          }
        ]
      }
    }
    

Was the article helpful?

Language / Region
© 2022 Yandex.Cloud LLC
In this article:
  • Before you start
  • Required paid resources
  • Prepare the environment
  • Create a service account
  • Create a YDB database
  • Create a table in the database
  • Create a function
  • Test the function