DEV Community

Cover image for Test your mySQL / Aurora database with Lambda
Nicolas Triantafillou for AWS Heroes

Posted on

Test your mySQL / Aurora database with Lambda

I couldn't find a completely straight forward lambda function, using NodeJS and AWS SDK v3 to test RDS.

So here's the one I cobbled together. It logs in and runs 'show tables'.

It requires a lambda layer with the mysql nodejs library.

import mysql from 'mysql';

var connection = mysql.createConnection({
    host: "blah.amazonaws.com",
    user: "userName",
    password: "password",
    database: "dbName",
    timezone: 'utc'
});

connection.connect();

export function handler(event, context, callback)  {

    connection.query('show tables', function (error, results, fields) {
        if (error) {
            connection.destroy();
            throw error;
        } else {
            // connected!
            console.log(results);
            callback(error, results);
            connection.end(function (err) { callback(err, results); });
        }
    });
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)