[英]ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt' When using AWS Lambda and Python
I'm currently trying to set up a basic Lambda function in python using AWS CDK and Python, and want to be able to include external libraries in my Lambda code.我目前正在尝试使用 AWS CDK 和 Python 在 python 中设置一个基本的 Lambda function,并希望能够在我的 Lambda 代码中包含外部库。 This is what I've got so far:
这是我到目前为止所得到的:
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,
)
Whenever I run cdk synth just for sanity, I'm getting this error: ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'.每当我为了理智运行 cdk synth 时,都会收到此错误:错误:无法打开需求文件:[Errno 2] 没有这样的文件或目录:'requirements.txt'。 I'm brand new to using docker and AWS Lambda, but I've seen something about creating a docker file and copying over files to the docker image in another post, although I'm not entirely sure if that applies when doing things with AWS as this source:
我是使用 docker 和 AWS Lambda 的新手,但我在另一篇文章中看到了一些关于创建 docker 文件并将文件复制到 docker 图像的内容,尽管我不完全确定这是否适用于使用 AWS 做事作为这个来源:
https://docs.aws.amazon.com/lambda/latest/dg/python-image.html https://docs.aws.amazon.com/lambda/latest/dg/python-image.html
says that "AWS provides a Dockerfile for each of the base images to help with bundling your container image".说“AWS 为每个基本映像提供 Dockerfile 以帮助捆绑您的容器映像”。 I have enabled file sharing for the top level project directory using docker, so I don't think that the issue.
我已经使用 docker 为顶级项目目录启用了文件共享,所以我认为这不是问题。 Also I'm a bit confused if I have to use Amazon ECR here or if this will allow me to include external dependencies in my Lambda code.
如果我必须在这里使用 Amazon ECR 或者这是否允许我在我的 Lambda 代码中包含外部依赖项,我也有点困惑。 I'm assuming I somehow have to just bring in the requirements.txt file into the docker image template provided by AWS, but not sure how to do that.
我假设我必须以某种方式将 requirements.txt 文件引入 AWS 提供的 docker 图像模板,但不确定如何操作。 Any help is greatly appreciated.
任何帮助是极大的赞赏。
can you try to add user="root"
as an option to the BundlingOptions?您可以尝试将
user="root"
添加为 BundlingOptions 的选项吗?
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"
],
user="root",
),
),
handler='hello.handler',
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.