applecrusher
applecrusher

Reputation: 5648

Cannot find MySQL in NodeJS using AWS Lambda

I have the following code that I would like to execute. I have tried requiring mysql and node-mysql and they both give me the same error:

Code:

var AWS = require("aws-sdk");
var mysql = require("mysql");


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

    try {

        console.log("GOOD");

    }

     catch (error) {
        context.fail(`Exception: ${error}`)
    }


};

Error:

{
  "errorMessage": "Cannot find module 'mysql'",
  "errorType": "Error",
  "stackTrace": [
    "Function.Module._load (module.js:417:25)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "Object.<anonymous> (/var/task/index.js:2:13)",
    "Module._compile (module.js:570:32)",
    "Object.Module._extensions..js (module.js:579:10)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)"
  ]
}

How do I import mysql into node using lambda or get this to work?

Upvotes: 2

Views: 11315

Answers (4)

UZOR
UZOR

Reputation: 1

I experienced this when using knex, although I had mysql in my package.json.

I had to require('mysql') in my lambda (or a file it references) so that Serverless packages it during deployment.

Upvotes: 0

Feng Zhang
Feng Zhang

Reputation: 1948

if using package.json, simply add below and run "npm install"

{ "dependencies": { "mysql": "2.12.0" } }

Upvotes: 0

harishanth raveendren
harishanth raveendren

Reputation: 604

In case any body needs an alternative,

You can use the cloud9 IDE which is free to open the lambda function and execute the npm init using the terminal window against the lambda function folder this will provide the node package file, which then can be used to install dependencies.

Upvotes: 1

Dishant Kapadiya
Dishant Kapadiya

Reputation: 553

Ohk so this is expected to happen.

The problem is that AWS Lambda runs on a different machine and there is no way you can configure that particular machine to run in a custom environment. You can however package the Node Module of mysql or node-mysql in a zip and upload to AWS Lambda. Steps are,

  1. npm install mysql --save
  2. Zip your folder and INCLUDING your node package
  3. Upload this zip file as your code in AWS Lambda.

You can also take a better approach by using Serverless Framework. More info here. In this approach, you write a YAML file which contains all the details and configuration you want to deploy your lambda with. Under your lambda configuration, specify path to your node module (say, nodemodule/**) under package -> include section. This will package your required alongwith your code. Later using command line you can deploy this lambda. It uses AWS Cloudformation service and is one of most prefered way of deploying resources.

More information on packaging using Serverless Framework can be found here.

Note: To use serverless framework there couple of steps like getting API keys for your user, setting right permissions in IAM etc. These are just initial setup and won't be need later. Do perform those prior to deploying using serverless framework.

Hope this helps!

Upvotes: 5

Related Questions