简体   繁体   中英

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. 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'. 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:

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". I have enabled file sharing for the top level project directory using docker, so I don't think that the issue. 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. 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. Any help is greatly appreciated.

can you try to add user="root" as an option to the 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',
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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