简体   繁体   English

AWS Lambda 函数如何在执行期间获取依赖项

[英]How AWS Lambda functions get the dependencies during execution

I have just started learning AWS and nodejs.我刚刚开始学习 AWS 和 nodejs。 I am trying to understand how this lambda in the serverless app gets the required information while execution.Let's consider the below piece of code我试图了解无服务器应用程序中的这个 lambda 在执行时如何获取所需的信息。让我们考虑下面的代码

use strict';

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

var lambda = new AWS.Lambda({
    apiVersion: '2015-03-31',
    endpoint: 'https://lambda.' + process.env.DYNAMODB_REGION + '.amazonaws.com',
    logger: console
});

So the first statement it creates a variable AWS.所以它的第一个语句创建了一个变量 AWS。 In any normal application these dependencies will be available in node modules and when we refer to it, we will easily access it.在任何正常的应用程序中,这些依赖项将在节点模块中可用,当我们引用它时,我们将很容易地访问它。 But here in the case of lambda function which has been created as a serverless app how it gets the dependency.但是在 lambda function 作为无服务器应用程序创建的情况下,它是如何获得依赖性的。

My second question is when we refer to process.env.DYNAMODB_REGION what would be the value for process.env?我的第二个问题是,当我们引用 process.env.DYNAMODB_REGION 时,process.env 的价值是什么?

My third question is it possible to create a common logger file, get it imported inside the lambda and use it to log the details?我的第三个问题是否可以创建一个通用记录器文件,将其导入 lambda 并使用它来记录详细信息?

Please help me to understand how lambda function get all these details.请帮助我了解 lambda function 如何获取所有这些详细信息。

  1. the aws-sdk dependency is provided by the Lambda runtime, so you don't have to download and package it yourself. aws-sdk依赖由 Lambda 运行时提供,所以你不必自己下载和 package 。 Any other dependency that's not provided by the runtime, you will have to package them in the zip file which you upload when creating your Lambda function.运行时未提供的任何其他依赖项,您必须在创建 Lambda function 时上传的 zip 文件中添加它们 package。

  2. DYNAMODB_REGION is not a standard environment variable that's set by the runtime; DYNAMODB_REGION不是由运行时设置的标准环境变量; you'd have to provide its value yourself when you create your lambda. For the list of environment variables that are set by the Lambda runtime, as well as how to set your own environment variables, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html .创建 lambda 时,您必须自己提供它的值。有关 Lambda 运行时设置的环境变量列表,以及如何设置您自己的环境变量,请参阅https://docs.aws.amazon .com/lambda/latest/dg/configuration-envvars.html

  3. Not exactly sure what you are asking, but for logging, the easiest way is to use AWS CloudWatch.不确定你在问什么,但对于日志记录,最简单的方法是使用 AWS CloudWatch。 For more information you can see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-logging.html .有关更多信息,请参阅https://docs.aws.amazon.com/lambda/latest/dg/nodejs-logging.html

The code you posted is accessing a lambda function within another lambda function. It is not exactly how you'd create your lambda function for your serverless app.您发布的代码正在访问另一个 lambda function 中的 lambda function。这并不是您为无服务器应用程序创建 lambda function 的方式。

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

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