IamCalvinBrown.com a Blog Site

Code Examples, Consulting Advice and lots of other cool things

AWS Connect to SQL Server using Lambda

/* there's a package that needs to be uploaded -- email calvin@kairuconsulting.com for this package. It's a zip that can be uploaded to AWS Lambda  */

const sql = require('mssql');

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

    const config = {

        user: 'your_user',

        password: 'your_password',

        server: 'abcd1234.us-east-1.rds.amazonaws.com',

        database: 'AdventureWork'

    };

    sql.connect(config, (err) => {

    

    if (err) 

    {

      console.log('Error ' + err);

      callback(err);

    } 

    else 

    {

        const req = new sql.Request();

        req.query('SELECT * FROM your_table', (error, result) => {

        if (error) 

        {

          console.log('Error' + error);

          callback(error);

        } 

        else 

        {

          console.log('Result' + result);

          sql.close();

          callback(null, result.recordset);

        }

      });

    }

  });

    

    //callback(null, "some success message");

};


Loading