简体   繁体   English

AWS Cloud9:在本地引用 Lambda 层

[英]AWS Cloud9: Referencing Lambda Layer Locally

I have set-up a Cloud9 environment to develop and test lambda functions.我已经设置了一个 Cloud9 环境来开发和测试 lambda 函数。 To make the environment "cleaner" I have opted to use lambda layers to specify the function's dependencies.为了使环境“更干净”,我选择使用 lambda 层来指定函数的依赖项。 By doing this, I have removed the dependency folders from the environment, but I am now unable to test locally.通过这样做,我已经从环境中删除了依赖文件夹,但我现在无法在本地进行测试。

For example, I have a lambda layer for Stripe's python library.例如,我有一个用于 Stripe 的 python 库的 lambda 层。 I am able to write a lambda function in Cloud9 referencing stripe, deploy the function, and successfully test the function remotely.我能够在 Cloud9 引用条带中编写一个 lambda 函数,部署该函数,并成功地远程测试该函数。 But i am unable to run that function locally, as I get "unable to import stripe"但是我无法在本地运行该功能,因为我“无法导入条纹”

Is there a way to test a lambda function, that depends on a lambda layer, locally by specifying the layer ARN in Cloud9?有没有办法通过在 Cloud9 中指定层 ARN 在本地测试依赖于 lambda 层的 lambda 函数?

不幸的是,AWS Cloud9 目前不支持 Lambda 层。

AWS Cloud9 documentation says that is not supported to execute functions with layers from the AWS Resources window. AWS Cloud9 文档说不支持从AWS 资源窗口执行带有层的函数。 But you can use sam local invoke or aws lambda on the Cloud9 terminal window.但是您可以在 Cloud9 终端窗口上使用sam local invokeaws lambda For instance例如

sam local invoke --event input.json --template ../template.yml <function_name>

will create a new docker image with the layer(s) dependencies that will be used to execute the lambda function.将使用将用于执行 lambda 函数的层依赖项创建一个新的 docker 镜像。 The TAG name is explained here 此处解释了 TAG 名称

master:~/environment/ahdv (master) $ docker images
REPOSITORY          TAG                                    IMAGE ID            CREATED             SIZE
samcli/lambda       nodejs8.10-03eb754e9966a1a2f789d500d   6b52bcffdc2e        About an hour ago   968MB
lambci/lambda       python3.6                              420212d009b3        3 weeks ago         1.03GB
lambci/lambda       python2.7                              7a436931435e        3 weeks ago         901MB
lambci/lambda       nodejs4.3                              c0914066d9a8        3 weeks ago         931MB
lambci/lambda       nodejs6.10                             74b405a65ed4        3 weeks ago         946MB
lambci/lambda       nodejs8.10                             edf1f613772c        3 weeks ago         960MB

There's an easy workaround to the problem of layers working from Lambda, but local testing failing:有一个简单的解决方法可以解决从 Lambda 工作的层的问题,但本地测试失败:

Add a folder to your python path before you import the modules contained in your layer, and simply have your Layer packages (unzipped) in that path.在导入图层中包含的模块之前,将文件夹添加到您的 python 路径,并且只需在该路径中放置您的图层包(解压缩)。

Here's an example where the "nltk" package is coming from a Layer.这是一个示例,其中“nltk”包来自层。 All layers in this project are stored in a "Layers" folder in the parent directory of the Lambda folders themselves, but you could do this from any level:此项目中的所有图层都存储在 Lambda 文件夹本身的父目录中的“图层”文件夹中,但您可以从任何级别执行此操作:

import sys
import os
sys.path.append(os.path.abspath("../Layers/custom_NLTK/python"))
import nltk

Hope this helps!希望这可以帮助!

I've also run into the same problem and ended up going with a workaround.我也遇到了同样的问题,最终找到了解决方法。 Might be useful for others.可能对其他人有用。

My use case was fairly simple, I needed to share some common code between several functions of an application.我的用例相当简单,我需要在应用程序的几个功能之间共享一些通用代码。 Lambda layers is the perfect solution, but I could not get the integration to work in the way I needed in cloud-9. Lambda 层是完美的解决方案,但我无法让集成以我在 cloud-9 中所需的方式工作。

I ended up not using lambda layers in the end.我最终没有使用 lambda 层。 I created a new folder (not a function) called common-code at the application level.我在应用程序级别创建了一个名为 common-code 的新文件夹(不是函数)。

I then created a hard link (not symbolic) for each of the files I needed in the common area from each of the functions然后,我为每个函数的公共区域中所需的每个文件创建了一个硬链接(非符号)

ln ../common-code/some-helper-functions.js some-helper-functions.js

Then the files get correctly packaged up by cloud-9 when deploying.然后文件在部署时被 cloud-9 正确打包。

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

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