node.js - 无服务器框架的最佳实践

标签 node.js amazon-dynamodb serverless-framework

我是无服务器框架的新手。 在学习无服务器最佳实践时。 here

我有一个关于“在您的 Lambda 代码之外初始化外部服务”的问题。 如何实现? 例如:handler.js 中的以下代码

const getOneUser = (event, callback) => {
  let response = null;
  // validate parameters
  if (event.accountid && process.env.SERVERLESS_SURVEYTABLE) {
    let docClient = new aws.DynamoDB.DocumentClient();
    let params = {
      TableName: process.env.SERVERLESS_USERTABLE,
      Key: {
        accountid: event.accountid,
      }
    };
    docClient.get(params, function(err, data) {
      if (err) {
        // console.error("Unable to get an item with the request: ", JSON.stringify(params), " along with error: ", JSON.stringify(err));
        return callback(getDynamoDBError(err), null);
      } else {
        if (data.Item) { // got response
          // compose response
          response = {
            accountid: data.Item.accountid,
            username: data.Item.username,
            email: data.Item.email,
            role: data.Item.role,
          };
          return callback(null, response);
        } else {
          // console.error("Unable to get an item with the request: ", JSON.stringify(params));
          return callback(new Error("404 Not Found: Unable to get an item with the request: " + JSON.stringify(params)), null);
        }
      }
    });
  }
  // incomplete parameters
  else {
    return callback(new Error("400 Bad Request: Missing parameters: " + JSON.stringify(event)), null);
  }
};

问题是如何在我的 Lambda 代码之外初始化 DynamoDB。

更新 2:

Is below code optimized?

处理程序.js

let survey = require('./survey');
module.exports.handler = (event, context, callback) => {
    return survey.getOneSurvey({
      accountid: event.accountid,
      surveyid: event.surveyid
    }, callback);
};

调查.js

let docClient = new aws.DynamoDB.DocumentClient();
module.exports = (() => {
  const getOneSurvey = (event, callback) {....
      docClient.get(params, function(err, data)...
      ....
  };

  return{
     getOneSurvey : getOneSurvey,
  }
})();

最佳答案

这是有问题的引述:

Initialize external services outside of your Lambda code

When using services (like DynamoDB) make sure to initialize outside of your lambda code. Ex: module initializer (for Node), or to a static constructor (for Java). If you initiate a connection to DDB inside the Lambda function, that code will run on every invoke.

换句话说,在同一个文件中,但在实际处理程序代码之前。

let docClient = new aws.DynamoDB...
...
const getOneUser = (event, callback) => {
....
  docClient.get(params, ...

当容器启动时,处理程序外部的代码就会运行。当后续函数调用重用同一个容器时,您可以通过不再实例化外部服务来节省资源和时间。容器经常被重用,但每个容器一次只能处理一个并发请求,它们被重用的频率和时间是你无法控制的……除非你更新功能,在这种情况下任何现有的容器将不再是重用,因为他们拥有旧版本的功能。

您的代码将按编写的方式运行,但未经过优化。

当前一代 Node.js Lambda 函数(Node 4.x/6.x)中出现的这种方法的警告是,一些对象——特别是那些创建与外部服务的文字持久连接的对象——将阻止事件循环不会变空(一个常见的例子是 mysql 数据库连接,它与服务器保持实时 TCP 连接;相比之下,DynamoDB“连接”实际上是无连接的,因为它的传输协议(protocol)是 HTTPS)。在这种情况下,您需要采取不同的方法或 allow lambda to not wait for an empty event loop在卡住容器之前,通过在调用回调之前将 context.callbackWaitsForEmptyEventLoop 设置为 false...但只有在需要时才这样做,并且只有在您完全理解它的含义时才这样做。将其设置为默认值,因为 Internet 上有人说这是个好主意,稍后可能会给您带来神秘的错误。

关于node.js - 无服务器框架的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38238496/

相关文章:

node.js - 如果状态为 302,axios 会进入 catch 函数而不是 then

node.js - 这种情况下如何进行容量控制呢?

amazon-web-services - DynamoDB 表建模。软删除解决方案

express - 使用无服务器框架(AWS Lambda/网关)、Express、Mongoose/MongoDB Atlas 的应用程序频繁超时

amazon-web-services - 如何安排 AWS Lambda cron 在 11 :30pm on the last day of the month? 运行

amazon-web-services - 无服务器框架 - 在 "self"源未找到值

node.js - Mongoose Date.now 时间不准确

node.js - 将数据实时流式传输到 Big Query(使用 Node)的最佳方式是什么?

javascript - Now.js 和安全

c# - 更改默认 DynamoDB .NET SDK 行为以将 DateTime 存储为 Unix 时间