How to Connect a Node.js Lambda Function to an External Database

AWS Lambda is a powerful serverless compute service. However, connecting to an external database (e.g., MySQL, PostgreSQL, or MongoDB) requires careful planning, especially regarding connections, timeouts, and scaling.

Step 1: Prerequisites

  • Set up your database: Ensure your external database is publicly accessible or connected via a private network (e.g., through a VPC).
  • Install AWS CLI: Use AWS CLI to configure your Lambda or database credentials.
  • Install dependencies: Have Node.js installed and the database driver (e.g., mysql2 or pg) ready.

Step 2: Create a Node.js Lambda Function

  • Initialize a Node.js project:
mkdir nodejs-lambda-database
cd nodejs-lambda-database
npm init -y
  • Install required packages:
    • From MySQL
npm install mysql2
  • For PostgreSQL:
npm install pg
  • For MongoDB:
npm install mongodb
  • Install AWS SDK (optional but comes pre-installed on Lambda):
npm install aws-sdk
  • Create your Lambda handler file (e.g., index.js):

Step 3: Example Lambda Function for MySQL

Here’s an example Lambda function to connect to a MySQL database:

Code (index.js)

const mysql = require('mysql2/promise');

exports.handler = async (event) => {
  const dbConfig = {
    host: 'your-database-host', // e.g., 'database-1.xxxx.us-east-1.rds.amazonaws.com'
    user: 'your-database-username',
    password: 'your-database-password',
    database: 'your-database-name',
  };

  let connection;

  try {
    // Connect to the database
    connection = await mysql.createConnection(dbConfig);

    // Query example
    const [rows] = await connection.execute('SELECT * FROM your_table LIMIT 10');

    // Return results
    return {
      statusCode: 200,
      body: JSON.stringify(rows),
    };
  } catch (error) {
    console.error('Database connection error:', error);
    return {
      statusCode: 500,
      body: JSON.stringify({ message: 'Error connecting to database', error }),
    };
  } finally {
    if (connection) {
      await connection.end(); // Close the connection
    }
  }
};

Step 4: Package and Deploy Lambda

  • Create a deployment package:
zip -r lambda.zip .
  • Deploy the Lambda function using AWS CLI:
aws lambda create-function \
  --function-name NodeJSLambdaDB \
  --runtime nodejs18.x \
  --role <your-lambda-execution-role-arn> \
  --handler index.handler \
  --zip-file fileb://lambda.zip

Step 5: Configure Lambda to Connect to the Database

  • 1. Environment Variables

Store sensitive information (e.g., database host, user, password) as environment variables in Lambda. Avoid hardcoding them into the code.

aws lambda update-function-configuration \
  --function-name NodeJSLambdaDB \
  --environment "Variables={DB_HOST=your-host,DB_USER=your-user,DB_PASSWORD=your-password,DB_NAME=your-db}"

In your code, access them like this:

const dbConfig = {
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
};
  • 2. VPC Configuration (Optional)

Step 6: Optimize for Scalability

  1. Connection Pooling

Avoid opening new connections for every Lambda invocation. Instead, use a connection pool to reuse connections.

Example with MySQL:

const mysql = require('mysql2/promise');

let connectionPool;

const getConnection = async () => {
  if (!connectionPool) {
    connectionPool = mysql.createPool({
      host: process.env.DB_HOST,
      user: process.env.DB_USER,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_NAME,
      waitForConnections: true,
      connectionLimit: 10, // Limit concurrent connections
    });
  }
  return connectionPool;
};

exports.handler = async (event) => {
  const pool = await getConnection();
  try {
    const [rows] = await pool.execute('SELECT * FROM your_table LIMIT 10');
    return {
      statusCode: 200,
      body: JSON.stringify(rows),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ message: 'Database error', error }),
    };
  }
};

2. Timeouts

Set Lambda and database timeouts appropriately. For example, if your database query takes too long, Lambda might terminate.

3. Security Best Practices

  • Use AWS Secrets Manager to store database credentials securely.
  • Use IAM roles for Lambda to restrict permissions.
  • Limit database user privileges to only the required operations.

Step 7: Testing the Lambda Function

  1. Test locally using the AWS SAM CLI:
sam local invoke --event event.json

2. Use the AWS Management Console or AWS CLI to trigger the function and verify the results.

Connecting a Node.js Lambda function to an external database requires proper configuration and optimization to handle scalability, security, and performance. Using connection pools, Secrets Manager, and VPCs ensures a robust setup, while best practices like environment variables and limited privileges keep your application secure.

Previous Article

Introducing Storage Browser for Amazon S3: Simplify Data Access in Your Web Apps

Next Article

How to Auto-Renew Let’s Encrypt Certificates with Certbot

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨