在AWS Lambda中共享代码 [英] Sharing code in AWS Lambda

查看:68
本文介绍了在AWS Lambda中共享代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AWS Lambda函数之间共享代码的首选方式是什么?

What is the prefered way to share code between AWS Lambda functions?

我有这样的结构:

  • 功能
    • a
      • node_modules
      • index.js
      • package.json
      • functions
        • a
          • node_modules
          • index.js
          • package.json
          • node_modules
          • index.js
          • package.json
          • node_modules
          • index.js
          • package.json

          这使每个函数都拥有自己的node_modules,我可以使用CLI打包整个程序.

          This let every function keep its own node_modules and I can package the whole thing with the CLI.

          但是需要共享的自定义代码呢?

          But what about custom code that needs to be shared?

          我可以require("../mylibrary"),但是package命令仍然不会包含它.

          I can require("../mylibrary") but the package command would still not include it.

          推荐答案

          如dmigo所述,Lambda层是可能的.这是一些使用Lambda层代码的 SAM 模板代码:

          As dmigo mentioned already it's possible with Lambda layers. Here is some SAM template code for using the Lambda Layer Code:

          Globals:
            Function:
              Runtime: nodejs8.10
          
          MyFunction:
              Type: AWS::Serverless::Function
              Properties:
                  CodeUri: a/
                  Handler: index.handler
                  Layers:
                  - !Ref MySharedLayer
          MySharedLayer:
              Type: AWS::Serverless::LayerVersion
              Properties:
                  LayerName: SharedLayer
                  Description: Some code to share with the other lambda functions
                  ContentUri: layer/
                  CompatibleRuntimes:
                      - nodejs8.10
                  RetentionPolicy: Retain
          

          这篇关于在AWS Lambda中共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆