简体   繁体   English

使用 AWS Lambda 在 NodeJS 中找不到 MySQL

[英]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:我试过需要 mysql 和 node-mysql ,它们都给我同样的错误:

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?如何使用 lambda 将 mysql 导入节点或使其工作?

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.问题在于 AWS Lambda 在不同的机器上运行,您无法配置该特定机器以在自定义环境中运行。 You can however package the Node Module of mysql or node-mysql in a zip and upload to AWS Lambda.但是,您可以将mysqlnode-mysql的节点模块打包到 zip 中并上传到 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.将此 zip 文件作为您在 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.在这种方法中,您编写了一个 YAML 文件,其中包含要用于部署 lambda 的所有详细信息和配置。 Under your lambda configuration, specify path to your node module (say, nodemodule/** ) under package -> include section.在您的 lambda 配置下,在package -> include部分下指定节点模块的路径(例如, nodemodule/** )。 This will package your required alongwith your code.这将打包您所需的代码。 Later using command line you can deploy this lambda.稍后使用命令行,您可以部署此 lambda。 It uses AWS Cloudformation service and is one of most prefered way of deploying resources.它使用 AWS Cloudformation 服务,是部署资源的首选方式之一。

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.注意:要使用无服务器框架,有几个步骤,例如为您的用户获取 API 密钥、在 IAM 中设置正确的权限等。这些只是初始设置,以后不需要。 Do perform those prior to deploying using serverless framework.在使用无服务器框架进行部署之前,请务必执行这些操作。

Hope this helps!希望这有帮助!

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.您可以使用 cloud9 IDE,它可以免费打开 lambda 函数并使用终端窗口对 lambda 函数文件夹执行 npm init 这将提供节点包文件,然后可用于安装依赖项。

if using package.json, simply add below and run "npm install"如果使用 package.json,只需在下面添加并运行“npm install”

{ "dependencies": { "mysql": "2.12.0" } } {“依赖项”:{“mysql”:“2.12.0”}}

I experienced this when using knex , although I had mysql in my package.json.我在使用knex时遇到了这种情况,尽管我的 package.json 中有mysql

I had to require('mysql') in my lambda (or a file it references) so that Serverless packages it during deployment.我必须在我的 lambda(或它引用的文件require('mysql')require('mysql') ,以便无服务器在部署期间对其进行打包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM