简体   繁体   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

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.

相关问题 如何解决 Django 部署中 requirements.txt 文件中的依赖冲突 - How to solve dependency conflicts in requirements.txt file in Django deployment 如何在apache airflow DAG中运行独立requirements.txt文件 - How to run independent requirements.txt file in apache airflow DAG GAE Python - 我的 requirements.txt 上的库未安装 - GAE Python - libs on my requirements.txt are not installed 我可以使用 AWS Lambda 来满足我的要求吗? - Can I use AWS Lambda for my requirements? AWS Lambda Opencv(“无法导入模块‘lambda_function’:libgthread-2.0.so.0:无法打开共享对象文件:没有这样的文件或目录”) - AWS Lambda Opencv ("Unable to import module 'lambda_function': libgthread-2.0.so.0: cannot open shared object file: No such file or directory") AWS Lambda 使用 python 读取 S3 存储桶中的文件 - AWS Lambda read a file in the S3 bucket using python 运行 Elastic Beanstalk CLI 时出现“FileNotFoundError: [Errno 2] No such file or directory” - Got `FileNotFoundError: [Errno 2] No such file or directory` when running Elastic Beanstalk CLI AWS lambda 无服务器中不存在此类文件错误 - AWS lambda no such file exists error in serverless 在 nix 中使用 buildGoPackage 时出现“没有这样的文件或目录”错误 - 'No such file or directory' error when using buildGoPackage in nix 如何访问上传到python aws lambda中的层的yaml文件 - How to access a yaml file uploaded to a layer in a python aws lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM