Connect to TiDB with mysql2 in AWS Lambda Function

TiDB is a MySQL-compatible database, AWS Lambda Function is a compute service, and mysql2 is a popular open-source driver for Node.js.

In this tutorial, you can learn how to use TiDB and mysql2 in AWS Lambda Function to accomplish the following tasks:

  • Set up your environment.
  • Connect to your TiDB cluster using mysql2.
  • Build and run your application. Optionally, you can find sample code snippets for basic CRUD operations.
  • Deploy your AWS Lambda Function.

AWS Lambda - 图1

Note

This tutorial works with TiDB Serverless and TiDB Self-Hosted.

Prerequisites

To complete this tutorial, you need:

If you don’t have a TiDB cluster, you can create one as follows:

If you don’t have a TiDB cluster, you can create one as follows:

If you don’t have an AWS account or a user, you can create them by following the steps in the Getting Started with Lambda guide.

Run the sample app to connect to TiDB

This section demonstrates how to run the sample application code and connect to TiDB.

AWS Lambda - 图2

Note

For complete code snippets and running instructions, refer to the tidb-samples/tidb-aws-lambda-quickstart GitHub repository.

Step 1: Clone the sample app repository

Run the following commands in your terminal window to clone the sample code repository:

  1. git clone git@github.com:tidb-samples/tidb-aws-lambda-quickstart.git
  2. cd tidb-aws-lambda-quickstart

Step 2: Install dependencies

Run the following command to install the required packages (including mysql2) for the sample app:

  1. npm install

Step 3: Configure connection information

Connect to your TiDB cluster depending on the TiDB deployment option you’ve selected.

  • TiDB Serverless
  • TiDB Self-Hosted
  1. Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.

  2. Click Connect in the upper right corner. A connection dialog is displayed.

  3. Ensure the configurations in the connection dialog match your operating environment.

    • Endpoint Type is set to Public
    • Branch is set to main
    • Connect With is set to General
    • Operating System matches your environment.

    AWS Lambda - 图3

    Note

    In Node.js applications, you don’t have to provide an SSL CA certificate, because Node.js uses the built-in Mozilla CA certificate by default when establishing the TLS (SSL) connection.

  4. Click Generate Password to create a random password.

    AWS Lambda - 图4

    Tip

    If you have generated a password before, you can either use the original password or click Reset Password to generate a new one.

  5. Copy and paste the corresponding connection string into env.json. The following is an example:

    1. {
    2. "Parameters": {
    3. "TIDB_HOST": "{gateway-region}.aws.tidbcloud.com",
    4. "TIDB_PORT": "4000",
    5. "TIDB_USER": "{prefix}.root",
    6. "TIDB_PASSWORD": "{password}"
    7. }
    8. }

    Replace the placeholders in {} with the values obtained in the connection dialog.

Copy and paste the corresponding connection string into env.json. The following is an example:

  1. {
  2. "Parameters": {
  3. "TIDB_HOST": "{tidb_server_host}",
  4. "TIDB_PORT": "4000",
  5. "TIDB_USER": "root",
  6. "TIDB_PASSWORD": "{password}"
  7. }
  8. }

Replace the placeholders in {} with the values obtained in the Connect window.

Step 4: Run the code and check the result

  1. (Prerequisite) Install the AWS SAM CLI.

  2. Build the bundle:

    1. npm run build
  3. Invoke the sample Lambda function:

    1. sam local invoke --env-vars env.json -e events/event.json "tidbHelloWorldFunction"
  4. Check the output in the terminal. If the output is similar to the following, the connection is successful:

    1. {"statusCode":200,"body":"{\"results\":[{\"Hello World\":\"Hello World\"}]}"}

After you confirm that the connection is successful, you can follow the next section to deploy the AWS Lambda Function.

Deploy the AWS Lambda Function

You can deploy the AWS Lambda Function using either the SAM CLI or the AWS Lambda console.

  1. (Prerequisite) Install the AWS SAM CLI.

  2. Build the bundle:

    1. npm run build
  3. Update the environment variables in template.yml:

    1. Environment:
    2. Variables:
    3. TIDB_HOST: {tidb_server_host}
    4. TIDB_PORT: 4000
    5. TIDB_USER: {prefix}.root
    6. TIDB_PASSWORD: {password}
  4. Set AWS environment variables (refer to Short-term credentials):

    1. export AWS_ACCESS_KEY_ID={your_access_key_id}
    2. export AWS_SECRET_ACCESS_KEY={your_secret_access_key}
    3. export AWS_SESSION_TOKEN={your_session_token}
  5. Deploy the AWS Lambda Function:

    1. sam deploy --guided
    2. # Example:
    3. # Configuring SAM deploy
    4. # ======================
    5. # Looking for config file [samconfig.toml] : Not found
    6. # Setting default arguments for 'sam deploy'
    7. # =========================================
    8. # Stack Name [sam-app]: tidb-aws-lambda-quickstart
    9. # AWS Region [us-east-1]:
    10. # #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
    11. # Confirm changes before deploy [y/N]:
    12. # #SAM needs permission to be able to create roles to connect to the resources in your template
    13. # Allow SAM CLI IAM role creation [Y/n]:
    14. # #Preserves the state of previously provisioned resources when an operation fails
    15. # Disable rollback [y/N]:
    16. # tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
    17. # tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
    18. # tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
    19. # tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
    20. # Save arguments to configuration file [Y/n]:
    21. # SAM configuration file [samconfig.toml]:
    22. # SAM configuration environment [default]:
    23. # Looking for resources needed for deployment:
    24. # Creating the required resources...
    25. # Successfully created!

Web console deployment

  1. Build the bundle:

    1. npm run build
    2. # Bundle for AWS Lambda
    3. # =====================
    4. # dist/index.zip
  2. Visit the AWS Lambda console.

  3. Follow the steps in Creating a Lambda function to create a Node.js Lambda function.

  4. Follow the steps in Lambda deployment packages and upload the dist/index.zip file.

  5. Copy and configure the corresponding connection string in Lambda Function.

    1. In the Functions page of the Lambda console, select the Configuration tab, and then choose Environment variables.
    2. Choose Edit.
    3. To add your database access credentials, do the following:
      • Choose Add environment variable, then for Key enter TIDB_HOST and for Value enter the host name.
      • Choose Add environment variable, then for Key enter TIDB_PORT and for Value enter the port (4000 is default).
      • Choose Add environment variable, then for Key enter TIDB_USER and for Value enter the user name.
      • Choose Add environment variable, then for Key enter TIDB_PASSWORD and for Value enter the password you chose when you created your database.
      • Choose Save.

Sample code snippets

You can refer to the following sample code snippets to complete your own application development.

For complete sample code and how to run it, check out the tidb-samples/tidb-aws-lambda-quickstart repository.

Connect to TiDB

The following code establish a connection to TiDB with options defined in the environment variables:

  1. // lib/tidb.ts
  2. import mysql from 'mysql2';
  3. let pool: mysql.Pool | null = null;
  4. function connect() {
  5. return mysql.createPool({
  6. host: process.env.TIDB_HOST, // TiDB host, for example: {gateway-region}.aws.tidbcloud.com
  7. port: process.env.TIDB_PORT ? Number(process.env.TIDB_PORT) : 4000, // TiDB port, default: 4000
  8. user: process.env.TIDB_USER, // TiDB user, for example: {prefix}.root
  9. password: process.env.TIDB_PASSWORD, // TiDB password
  10. database: process.env.TIDB_DATABASE || 'test', // TiDB database name, default: test
  11. ssl: {
  12. minVersion: 'TLSv1.2',
  13. rejectUnauthorized: true,
  14. },
  15. connectionLimit: 1, // Setting connectionLimit to "1" in a serverless function environment optimizes resource usage, reduces costs, ensures connection stability, and enables seamless scalability.
  16. maxIdle: 1, // max idle connections, the default value is the same as `connectionLimit`
  17. enableKeepAlive: true,
  18. });
  19. }
  20. export function getPool(): mysql.Pool {
  21. if (!pool) {
  22. pool = connect();
  23. }
  24. return pool;
  25. }

Insert data

The following query creates a single Player record and returns a ResultSetHeader object:

  1. const [rsh] = await pool.query('INSERT INTO players (coins, goods) VALUES (?, ?);', [100, 100]);
  2. console.log(rsh.insertId);

For more information, refer to Insert data.

Query data

The following query returns a single Player record by ID 1:

  1. const [rows] = await pool.query('SELECT id, coins, goods FROM players WHERE id = ?;', [1]);
  2. console.log(rows[0]);

For more information, refer to Query data.

Update data

The following query adds 50 coins and 50 goods to the Player with ID 1:

  1. const [rsh] = await pool.query(
  2. 'UPDATE players SET coins = coins + ?, goods = goods + ? WHERE id = ?;',
  3. [50, 50, 1]
  4. );
  5. console.log(rsh.affectedRows);

For more information, refer to Update data.

Delete data

The following query deletes the Player record with ID 1:

  1. const [rsh] = await pool.query('DELETE FROM players WHERE id = ?;', [1]);
  2. console.log(rsh.affectedRows);

For more information, refer to Delete data.

Useful notes

  • Using connection pools to manage database connections can reduce the performance overhead caused by frequently establishing and destroying connections.
  • To avoid SQL injection, it is recommended to use prepared statements.
  • In scenarios where there are not many complex SQL statements involved, using ORM frameworks like Sequelize, TypeORM, or Prisma can greatly improve development efficiency.
  • For building a RESTful API for your application, it is recommended to use AWS Lambda with API Gateway.
  • For designing high-performance applications using TiDB Serverless and AWS Lambda, refer to this blog.

Next steps

Need help?

Ask questions on the Discord, or create a support ticket.