alionthego
alionthego

Reputation: 9713

how can I create one lambda function to get a mysql pool connection and provide that to other lambda functions?

I have a dozen or so lambda functions in node.js which are primarily used for mysql database queries.

At the moment I am opening and closing connections to my database in each of those functions.

Is it possible to have one function that just creates the connection (preferably pooled connection) and provides that to the other functions?

Additionally I am using the Parameter Store to store my secrets. What I am doing now is:

const AWS = require('aws-sdk');
var mysql = require('mysql');
var ssm = new AWS.SSM({ apiVersion: '2014-11-06' });

exports.handler = (event, context, callback) => {

    var databaseName;
    var databaseHost;
    var databaseUser;
    var databasePassword;

    var params = {
        Names: ['/databaseName', '/databaseHost', '/databaseUser', '/databasePassword'],
        WithDecryption: true
    };

    ssm.getParameters(params, function (err, data) {
        if (err) {
            callback(err);
        } else {
            var parameters = data.Parameters;
            parameters.forEach(function (value) {
                switch (value.Name) {
                case "/databaseName":
                    databaseName = value.Value;
                    break;
                case "/databaseHost":
                    databaseHost = value.Value;
                    break;
                case "/databaseUser":
                    databaseUser = value.Value;
                    break;
                case "/databasePassword":
                    databasePassword = value.Value;
                    break;
                }
            });

            var pool = mysql.createPool({
                host: databaseHost,
                user: databaseUser,
                password: databasePassword,
                database: databaseName,
                connectionLimit: 20
            });

            pool.getConnection(function (err, connection) {
                if (err) {
                    callback(err);
                } else {
                    var sql = "...";
                    var inserts = [...];

                    connection.query(sql, inserts, function (error, results, fields) {

                        connection.release();

                        if (error) {
                            callback(error);
                        } else {
                            callback(null, results);
                        }
                    });
                }
            });


        }
    });
};

What I would like is for the database connection to be in a separate lambda function and provide the pool connections without opening and closing the connection every time I call that function.

Upvotes: 0

Views: 824

Answers (1)

Sandeep Singh
Sandeep Singh

Reputation: 783

When it comes to AWS Lambda functions, you should keep these important points in mind before designing the solution

  1. Execution of functions happen in their own sandboxed containers
  2. It is not necessary for an execution environment to stay up all the time

So from the first point, we can conclude that resources cannot be shared between two functions and even two executions of the same function will not be able to do so and from the second point, we can conclude that even if you create a connection pool (which I do not suggest) it will be destroyed when that container will go down when it is not serving any requests.

Upvotes: 1

Related Questions