2

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?

Arafat Nalkhande
  • 11,078
  • 9
  • 39
  • 63
applecrusher
  • 5,508
  • 5
  • 39
  • 89

4 Answers4

5

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!

Dishant Kapadiya
  • 523
  • 4
  • 10
  • 1
    It does. Thanks! I just wish Lambda would have been able to detect external libraries in the inline editor. I made a false assumption it was like aws, already preconfigured in there. – applecrusher Jul 20 '17 at 02:52
1

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.

0

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

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

Feng Zhang
  • 1,698
  • 1
  • 17
  • 20
0

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.

UZOR
  • 1