繁体   English   中英

错误:无法打开需求文件:[Errno 2] 没有这样的文件或目录:'requirements.txt' 使用 AWS Lambda 和 Python 时

[英]ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt' When using AWS Lambda and Python

我目前正在尝试使用 AWS CDK 和 Python 在 python 中设置一个基本的 Lambda function,并希望能够在我的 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 图像模板,但不确定如何操作。 任何帮助是极大的赞赏。

您可以尝试将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.

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