简体   繁体   English

AWS Lambda:NodeJs 自定义层

[英]AWS Lambda: NodeJs custom layer

I'm new in nodejs and aws lambda.我是 nodejs 和 aws lambda 的新手。 I'm trying to create layer with common functions for my bunch of lambda functions.我正在尝试为我的一堆 lambda 函数创建具有常用函数的层。 In lambda handler the import of custom module has the following definition: let commonService = require('@common/service');在 lambda 处理程序中,自定义模块的导入具有以下定义: let commonService = require('@common/service');

The zip with the module file has the following structure:带有模块文件的 zip 结构如下:

nodejs
--node_modules
  --@custom
    --service
    --index.js
    --package.json  

But I get the error: "errorMessage": "Error: Cannot find module '@common/service'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",但我收到错误: "errorMessage": "Error: Cannot find module '@common/service'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",

The @custom/service module package.json contains the following: @custom/service模块 package.json 包含以下内容:

{
  "name": "@common/service",
  "version": "1.0.0",
  "main": "index.js",
  "license": "ISC",
  "dependencies": {
    /***
  }
}

Thanks in advance!提前致谢!

Try using AWS Lambda Layers .尝试使用AWS Lambda 层

Using Layers has two major benefits.使用图层有两个主要好处。

  • It allows you to deploy your base dependencies in one place and manage them centrally.它允许您将基本依赖项部署在一个地方并集中管理它们。
  • It shrinks the size of your Lambda app deployments.它缩小了 Lambda 应用程序部署的大小。 No more massive node_modules folder hierarchy to upload every time.不再需要每次上传大量的node_modules文件夹层次结构。

How to use them?如何使用它们? It's a rather in-depth topic, but the gist of it is that you can:这是一个相当深入的话题,但它的要点是您可以:

  1. build and deploy a Layer in AWS.在 AWS 中构建和部署一个层。 A Layer is nothing more than a "bare" npm project, really.一个层只不过是一个“裸”的 npm 项目,真的。 You install your commonly-used NodeJS modules in it, nothing else.您在其中安装常用的 NodeJS 模块,仅此而已。
  2. Assuming you're using the serverless application model to deploy your AWS Lambda functions, you configure your Lambda function to use the Layer.假设您使用无服务器应用程序 model 来部署您的 AWS Lambda 功能,您将 Lambda ZC1C425268E68385D11ABZ 配置为使用 07 层 7 层 7。 Here's a stripped-down version of a SAM yaml file that illustrates this:这是 SAM yaml 文件的精简版本,它说明了这一点:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: My Serverless App
Parameters:
  # ...

Globals:
  Function:
    Runtime: nodejs10.x
  #   Environment:
  #     Variables:
  #       NODE_ENV: test

Resources:

  CommonDependenciesLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: my-common-deps
      Description: Common dependencies for my NodeJS apps
      ContentUri: ../common-deps/
      RetentionPolicy: Retain
    Metadata:
      BuildMethod: nodejs10.x

  performSomeFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: performSomeFunction
      Handler: lambda.someFunction
      Layers:
        # You can either Ref a layer defined in this SAM file like this.
        # - !Ref CommonDependenciesLayer

        # ... or you can hard-code an external ARN
        # - arn:aws:lambda:us-east-1:nnnnnnnnnnnnn:layer:myapp-common-deps:5
      CodeUri: ./
      # ...
  1. In your Lambda code, all you do is use the normal CJS require syntax.在您的 Lambda 代码中,您所做的就是使用普通的 CJS 要求语法。 For example, suppose I put the mysql library in my CommonDependenciesLayer , above.例如,假设我将mysql库放在上面的CommonDependenciesLayer中。 As long as my Lambda function points to the layer in the SAM file, all I have to do is require('mysql') in my code.只要我的 Lambda function 指向 SAM 文件中的层,我所要做的就是在我的代码中使用require('mysql') It'll be there.它会在那里。

As you get it working, there are some subtleties to think about with respect to Layer deployment.当你开始工作时,有一些关于 Layer 部署的微妙之处需要考虑。

  • Do I want to build the Layer every time?我想每次都构建图层吗? (Probably not, but I recommend you do this as you're learning how Layers and your Lambdas work together). (可能不会,但我建议您在学习 Layers 和 Lambdas 如何协同工作时这样做)。
  • How often do my Layer components change?我的图层组件多久更改一次?
  • Do I want to make my Layer publicly available?我想公开我的图层吗?
  • Do I want to user a publicly available Layer from another account?我想从另一个帐户使用公开可用的图层吗?

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

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