Home > Uncategorized > Connect to #MSSQL from #AWS #LAMBDA using #NODE

Connect to #MSSQL from #AWS #LAMBDA using #NODE


In order to connect to Microsoft SQL server from Node in AWS Lambda you need to include a reference to require(‘mssql’); – but how do you install NPM packages on AWS Lambda.

That’s where the AWS CLI comes in. Download it, and the first thing you do is configure it with an IAM user as follows

aws configure

which prompts;

AWS Access Key ID [****************]:
AWS Secret Access Key [****************]:
Default region name [None]: eu-west-1
Default output format [None]:

Now, get a list of lambda functions as follows;

aws lambda list-functions

You can get the function name from the json returned, then if you want to get a zip of your function call;

aws lambda get-function –function-name <Function Name>

The JSON will return a property “Location” which you use to download the zip file.  Unzip the zip file, and navigate into the folder, then type

npm install mssql

Once complete, zip up the folder, and upload it back to AWS as follows

aws lambda update-function-code –function-name <Function Name> –zip-file “fileb://<ZIP Name>”

Now, you can create a file, database.js in your Lambda project as follows

const sql = require(‘mssql’);

(function() {

// ——- Database support ——- //

module.exports.execute = function(sqlQuery, callback)
const config = {
user: ‘xxxxxx’,
password: ‘xxxxx’,
server: ‘xxxx.xx.xx.xxx’,
database: ‘xxxxxx’,
sql.connect(config, (err) => {
if (err) {
} else {
const req = new sql.Request();
req.query(sqlQuery, (error, result) => {
if (error) {
} else {
callback(null, result.recordset);

sql.on(‘error’, (err) => {




Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: