lambda ::
When Python support was announced for AWS Lambda at re:Invent, we were excited to start using it. As you could see with our emoji-powered Votebot for Slack, we put it to quick use, converting our skirmishes about lunch orders into a peaceful, democratic process.
当re:Invent宣布对AWS Lambda提供Python支持时 ,我们很高兴开始使用它。 如您所见, 我们的表情符号支持的Slack投票工具Votebot可以快速使用,将午餐订单方面的小冲突转变为和平,民主的过程。
Building Lambda functions is great, but there can be some challenges during development. There are many that we may yet cover, but the one we wanted to start with was just writing and debugging the function. As such, we wanted to build a local harness for lambda functions that could shorten the feedback loop on development, be used for attaching debuggers, drive test data through the function, and profile it as well so we’d have an idea what it would cost to run.
构建Lambda函数很棒,但是在开发过程中可能会遇到一些挑战。 我们可能涵盖了许多内容,但是我们想开始的只是编写和调试函数。 因此,我们想为lambda函数构建一个本地工具,该函数可以缩短开发过程中的反馈循环,可用于附加调试器,通过该函数驱动测试数据并对其进行概要分析,因此我们将了解其功能。运行成本。
To do this, we developed Emulambda. To be clear, that’s a portmanteau of the words “emulator” and “lambda,” but if you want to think it’s some sort of anonymous emu, go for it! Emulambda, just like AWS Lambda, works with Python 2.7.
为此,我们开发了Emulambda。 明确地说,这是“仿真器”和“ lambda”两个词的组合,但是如果您想以为它是某种匿名e,那就去吧! Emulambda与AWS Lambda一样,都可与Python 2.7一起使用。
Installing Emulambda is simple:
安装Emulambda很简单:
git clone
the Emulambda repo- Install it with
pip install -e emulambda
(You might need to sudo this command if you’re using your system Python instead of a virtualenv or similar.)
-
git clone
Emulambda回购 - 使用
pip install -e emulambda
安装它(如果使用系统Python而不是virtualenv或类似的软件,则可能需要sudo此命令。)
These steps should make the emulambda
script available as a runnable file in your path. The -e
argument means the installation is “editable,” so that you can hack on it if you need or want to. Soon, we’ll put the project in PyPi and update these instructions to be even easier.
这些步骤应使emulambda
脚本在您的路径中作为可运行文件可用。 -e
参数表示安装是“可编辑的”,因此您可以根据需要修改该安装。 很快,我们会将项目放入PyPi,并更新这些说明以使其更加容易。
The repository comes with an example lambda function and example event document to show you exactly how this works. The simplest way to run Emulambda is to simply give an import path for your function and an event JSON document. You can run the example from the repository root with this command:
该存储库附带一个示例lambda函数和示例事件文档,以向您确切说明其工作方式。 运行Emulambda的最简单方法是简单地为函数和事件JSON文档提供导入路径。 您可以使用以下命令从资源库根目录运行示例:
emulambda example.example_handler example/example.json
emulambda example.example_handler example/example.json
This will print the return value of the function to stdout, like so:
这会将函数的返回值打印到stdout,如下所示:
If you want a little more information, add the -v
switch.
如果需要更多信息,请添加-v
开关。
Executed example.example_handler Estimated... ...execution clock time: 157ms (200ms billing bucket) ...execution peak RSS memory: 368M (385888256 bytes) ----------------------RESULT---------------------- value1
Executed example.example_handler Estimated... ...execution clock time: 157ms (200ms billing bucket) ...execution peak RSS memory: 368M (385888256 bytes) ----------------------RESULT---------------------- value1
Now, we have an idea what our function takes to run. You can also set a timeout for the function, just as you can in AWS, so that tests can fail if the function takes too long. The timing and memory usage measurements are based on informed guesses about how AWS determines these things, but they should be viewed as approximate at best. Over time we might be able to refine these estimates. You can find out more about the profiling mechanism in the project README.
现在,我们知道了函数需要执行的操作。 您也可以像在AWS中一样为该函数设置超时,以便如果该函数花费太长时间,测试可能会失败。 时间和内存使用情况的测量基于有关AWS如何确定这些事情的明智猜测,但充其量应视为最佳。 随着时间的流逝,我们也许可以完善这些估计。 您可以在项目README中找到有关配置机制的更多信息。
Another cool thing you can do is send the event into the function via stdin
. Just use the -
(dash) character in place of a filename, like this:
您可以做的另一件很酷的事情是通过stdin
将事件发送到函数中。 只需使用-
(破折号)字符代替文件名即可,如下所示:
Since stdin is supported by… well, everything that uses stdout, you can easily integrate Emulambda with other scripts or utilities.
由于stdin受……好,所有使用stdout的东西都支持,因此您可以轻松地将Emulambda与其他脚本或实用程序集成。
The input document can be either a single JSON document, or a Line-Delimited JSON (LDJSON) stream. In either case, a file or stdin can be used. For more information on the LDJSON stream support, see the README.
输入文档可以是单个JSON文档,也可以是行分隔JSON(LDJSON)流。 无论哪种情况,都可以使用文件或标准输入。 有关LDJSON流支持的更多信息, 请参见README 。
We hope Emulambda is useful to you at both development and test time when you’re working with AWS Lambda.
我们希望Emulambda在使用AWS Lambda时在开发和测试时对您都有用。
For development, you can
为了发展,你可以
- run your lambda functions instantly locally, without packaging and sending to AWS;
- shorten your feedback loop on lambda executions; and
- easily attach debuggers to your lambda.
- 无需打包并发送到AWS即可在本地立即运行lambda函数;
- 缩短关于lambda执行的反馈循环; 和
- 轻松将调试器附加到您的lambda。
And for testing, Emulambda lets you
为了进行测试,Emulumbda可让您
- easily integrate with test tools using a simple CLI and various input methods;
- use stream mode to test many cases or run fuzz tests; and
- use profiling information to identify expensive/problematic lambdas early.
- 使用简单的CLI和各种输入法轻松地与测试工具集成;
- 使用流模式来测试许多情况或运行模糊测试; 和
- 使用配置文件信息尽早识别昂贵/有问题的lambda。
Some future features we’d like to see in the project:
我们希望在项目中看到一些将来的功能:
- SQS Support (though for now, you can easily pipe AWS CLI output to Emulambda stdin)
- Kinesis support / emulation
- An AWS event library, for common integrations with other services
- Context support! Probably an optional argument to provide context objects in the same way as event objects.
- SQS支持(尽管到目前为止,您可以轻松地将AWS CLI输出通过管道传输到Emulambda stdin)
- Kinesis支持/仿真
- 一个AWS事件库,用于与其他服务的常见集成
- 上下文支持! 可能是一个可选参数,用于以与事件对象相同的方式提供上下文对象。
We are excited about the opportunities in serverless computing, and how Fugue will support you in pursuing those opportunities in the near future.
我们对无服务器计算方面的机会以及Fugue将如何在不久的将来支持您的机会感到兴奋。
翻译自: https://www.pybloggers.com/2015/10/its-an-emulator-not-a-petting-zoo-emu-and-lambda/
lambda ::