使用Bazel为AWS Lambda创建Python Zip
我有一个包含一组python aws lambdas的monorepo,我正在使用 bazel> bazel 用于建造和包装Lambdas 。我现在正在尝试使用Bazel创建一个遵循预期 aws lambdas包装我可以上传到lambda。想知道与巴泽尔一起做这件事的最佳方法是什么?
以下是我迄今为止尝试过的几件不同的事情:
尝试1:py_binary
build.bazel
py_binary(
name = "main_binary",
srcs = glob(["*.py"]),
main = "main.py",
visibility = ["//appcode/api/transaction_details:__subpackages__"],
deps = [
requirement("Faker"),
],
)
问题:
这会生成以下内容:
- main_binary( Python可执行文件)
- main_binary.runfiles
- main_binary.runfiles_manifest
lambda希望处理程序的格式为lambda_function.lambda_handler
。由于main_binary
是可执行文件和python文件,因此它不会公开实际的处理程序方法,而lambda则炸毁了,因为它找不到它。我尝试更新处理程序配置,以简单地指向main_binary
,但它会炸毁,因为它期望两个参数(即lambda_function.lambda_handler
)。
尝试2:py_library + pkg_zip
build.bazel
py_library(
name = "main",
srcs = glob(["*.py"]),
visibility = ["//appcode/api/transaction_details:__subpackages__"],
deps = [
requirement("Faker"),
],
)
pkg_zip(
name = "main_zip",
srcs =["//appcode/api/transaction_details/src:main" ],
)
问题:
这将生成一个zip文件,并使用:
- main.py
__ INT __ INT__。py
zip文件现在包括main.py
,但其运行时依赖性都不是。因此,Lambda炸毁了,因为它找不到faker
。
其他尝试:
我也尝试使用-build_python_zip
flag以及@bazel_tools // tools/zip:拉链
具有通用规则,但它们都与以前的两次尝试相似。
I've a monorepo that contains a set of Python AWS lambdas and I'm using Bazel for building and packaging the lambdas. I'm now trying to use Bazel to create a zip file that follows the expected AWS Lambdas packaging and that I can upload to Lambda. Wondering what's the best way to do this with Bazel?
Below are a few different things I've tried thus far:
Attempt 1: py_binary
BUILD.bazel
py_binary(
name = "main_binary",
srcs = glob(["*.py"]),
main = "main.py",
visibility = ["//appcode/api/transaction_details:__subpackages__"],
deps = [
requirement("Faker"),
],
)
Problem:
This generates the following:
- main_binary (python executable)
- main_binary.runfiles
- main_binary.runfiles_manifest
Lambda expects the handler to be in the format of lambda_function.lambda_handler
. Since main_binary
is an executable vs. a python file, it doesn't expose the actual handler method and the lambda blows up because it can't find it. I tried updating the handler configuration to simply point to the main_binary
but it blows up because it expects two arguments(i.e. lambda_function.lambda_handler
).
Attempt 2: py_library + pkg_zip
BUILD.bazel
py_library(
name = "main",
srcs = glob(["*.py"]),
visibility = ["//appcode/api/transaction_details:__subpackages__"],
deps = [
requirement("Faker"),
],
)
pkg_zip(
name = "main_zip",
srcs =["//appcode/api/transaction_details/src:main" ],
)
Problem:
This generates a zip file with:
- main.py
__init__.py
The zip file now includes the main.py
but none of its runtime dependencies. Thus the lambda blows up because it can't find Faker
.
Other Attempts:
I've also tried using the --build_python_zip
flag as well as the @bazel_tools//tools/zip:zipper
with a generic rule but they both lead to similar outcomes as the two previous attempts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们使用
@bazel_tools //工具/zip:zipper
带有自定义规则。我们还将serverless
拉动rules_nodejs
并通过Bazel运行,这会导致软件包构建在运行SLS部署之前发生。
我们使用
pip_parse
来自rules_python
。我不确定下面的_SHORT_PATH
功能是否适用于pip_install
或其他机制。尽管尴尬,但支持文件过滤。理想情况下,拉链生成将通过单独的二进制(即,一个python脚本)来处理,该脚本将允许使用正则表达式/globs/等进行过滤。 Bazel不支持Starlark中的正则表达式,因此我们使用自己的东西。
我包括了一个摘录:
lambda.bzl
build.bazel
server> serverless.yml
We use
@bazel_tools//tools/zip:zipper
with a custom rule. We also pullserverless
in usingrules_nodejs
and run it through bazel, which causes the package building to happen prior to runningsls deploy
.We use
pip_parse
fromrules_python
. I'm not sure whether the_short_path
function below will work withpip_install
or other mechanisms.File filtering is supported, although it's awkward. Ideally the zip generation would be handled by a separate binary (i.e., a Python script) which would allow filtering using regular expressions/globs/etc. Bazel doesn't support regular expressions in Starlark, so we use our own thing.
I've included an excerpt:
lambda.bzl
BUILD.bazel
serverless.yml
以下是我对上一个答案的更改以生成lambda zip。感谢@jvolkman的原始建议。
生成
sup者来
project/build.bazel: 添加了从
project> project> project/unignts.txt
insuert_lost> project/workspace.bazel
:与pip_parse
proges/build_rules/lambda_packaging/lambda.bzl交换pip_install: 由修改的自定义规则由@jvolkman将源代码包含在生成的邮政编码中。
project/appcode/api/transaction_details/src/build.bazel :使用自定义
py_lambda_zip
py_library py_lambda_zipBelow are the changes I made to the previous answer to generate the lambda zip. Thanks @jvolkman for the original suggestion.
project/BUILD.bazel: Added rule to generate
requirements_lock.txt
fromproject/requirements.txt
project/WORKSPACE.bazel: swap pip_install with pip_parse
project/build_rules/lambda_packaging/lambda.bzl: Modified custom rule provided by @jvolkman to include source code in the resulting zip code.
project/appcode/api/transaction_details/src/BUILD.bazel: Used custom
py_lambda_zip
rule to zip uppy_library