【发布时间】:2022-12-30 18:33:28
【问题描述】:
我目前正在尝试使用 AWS CDK 和 Python 在 python 中设置一个基本的 Lambda 函数,并希望能够在我的 Lambda 代码中包含外部库。这是我到目前为止所得到的:
from constructs import Construct
import aws_cdk as core
from aws_cdk import (
Stack,
aws_lambda as _lambda,
aws_apigateway as apigw,
)
class SportsTeamGeneratorStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
my_lambda = _lambda.Function(self, 'HelloHandler',
runtime=_lambda.Runtime.PYTHON_3_9,
code=_lambda.Code.from_asset("lambda",
bundling= core.BundlingOptions(
image=_lambda.Runtime.PYTHON_3_9.bundling_image,
command=[
"bash", "-c",
"pip install --no-cache -r requirements.txt -t /asset-output && cp -au . /asset-output"
],
),
),
handler='hello.handler',
)
apigw.LambdaRestApi(
self, 'Endpoint',
handler=my_lambda,
)
每当我为了理智而运行 cdk synth 时,都会收到此错误:错误:无法打开需求文件:[Errno 2] 没有这样的文件或目录:'requirements.txt'。我是使用 docker 和 AWS Lambda 的新手,但我在另一篇文章中看到了一些关于创建 docker 文件并将文件复制到 docker 镜像的内容,尽管我不完全确定这是否适用于使用 AWS 做事作为这个来源:
https://docs.aws.amazon.com/lambda/latest/dg/python-image.html
说“AWS 为每个基本映像提供了一个 Dockerfile,以帮助捆绑您的容器映像”。我已经使用 docker 为顶级项目目录启用了文件共享,所以我认为这不是问题。如果我必须在这里使用 Amazon ECR,或者这是否允许我在我的 Lambda 代码中包含外部依赖项,我也有点困惑。我假设我必须以某种方式将 requirements.txt 文件引入 AWS 提供的 docker 图像模板中,但不确定该怎么做。任何帮助是极大的赞赏。
【问题讨论】:
-
那么,您的资产文件夹中是否有
requirements.txt文件? -
就我而言,requirements.txt 文件是否必须放在“lambda”文件夹中?
-
是的,它应该存在于资产文件夹中。
标签: python amazon-web-services docker aws-lambda aws-cdk