简体   繁体   English

AWS Lambda “Lambda 找不到文件 lambda_function.py。确保您的处理程序支持以下格式:file-name.method。” 错误

[英]AWS Lambda "Lambda can't find the file lambda_function.py. Make sure that your handler upholds the format: file-name.method." error

I am getting "Lambda can't find the file lambda_function.py. Make sure that your handler upholds the format: file-name.method."我收到“Lambda 找不到文件 lambda_function.py。确保您的处理程序支持以下格式:file-name.method。” error just above the AWS Lambda function code block. AWS Lambda 函数代码块正上方的错误。 You can see the warning here你可以在这里看到警告

But as you can see in this code block, my file name is lambda_function and my function name is lambda_handler.但是正如您在此代码块中看到的,我的文件名为 lambda_function,函数名为 lambda_handler。

import boto3
import os
import logging
import uuid
from webdriver_screenshot import WebDriverScreenshot

logger = logging.getLogger()
logger.setLevel(logging.INFO)

s3 = boto3.client('s3')

def lambda_handler(event, context):
    logger.info('## ENVIRONMENT VARIABLES')
    logger.info(os.environ)
 
    screenshot_file = "{}-{}".format(''.join(filter(str.isalpha, os.environ['URL'])), str(uuid.uuid4()))
    driver = WebDriverScreenshot()

    logger.info('Generate fixed height screenshot')
    driver.save_screenshot(os.environ['URL'], '/tmp/{}-fixed.png'.format(screenshot_file), height=1024)

    logger.info('Generate full height screenshot')    
    driver.save_screenshot(os.environ['URL'], '/tmp/{}-full.png'.format(screenshot_file))

    driver.close()

    if all (k in os.environ for k in ('BUCKET','DESTPATH')):
        ## Upload generated screenshot files to S3 bucket.
        s3.upload_file('/tmp/{}-fixed.png'.format(screenshot_file), 
                    os.environ['BUCKET'], 
                    '{}/{}-fixed.png'.format(os.environ['DESTPATH'], screenshot_file))
        s3.upload_file('/tmp/{}-full.png'.format(screenshot_file), 
                    os.environ['BUCKET'], 
                    '{}/{}-full.png'.format(os.environ['DESTPATH'], screenshot_file))

My code folder is like this and the main python code is in src folder .我的代码文件夹是这样的,主要的python代码在src文件夹中 In AWS Lambda, my folder is looking like this .在 AWS Lambda 中,我的文件夹如下所示 I do not understand what is the problem.我不明白是什么问题。

Even worse, when I try to test the code in AWS Lambda environment, this error is showing up.更糟糕的是,当我尝试在 AWS Lambda 环境中测试代码时,出现此错误 It says:它说:

Unable to import module 'lambda_function': No module named 'boto3'无法导入模块“lambda_function”:没有名为“boto3”的模块

But I have the boto3 module in the layer , it is uploaded from my S3 Bucket.但是我在层中有 boto3 模块,它是从我的 S3 Bucket 上传的。 It is succesfully added to the function已成功添加到函数中

The only thing I am suspecting, I may wrote environment variables wrong as shown here: environment variables due to I am a beginner in coding and AWS Lambda, I just copied both the PATH and PYTHONPATH from somewhere.我唯一怀疑的是,我可能写错了环境变量,如下所示:环境变量由于我是编码和 AWS Lambda 的初学者,我只是从某个地方复制了 PATH 和 PYTHONPATH。

I don't know what to do next and need some support here.我不知道接下来要做什么,需要一些支持。

Thanks.谢谢。

I faced the same problem when I tried to use an external module in lambda_function, in my case was OpenCV as lambda layer.当我尝试在 lambda_function 中使用外部模块时,我遇到了同样的问题,在我的例子中是 OpenCV 作为 lambda 层。 For making Opencv to work I have to change environment variables, making the key PYTHONPATH to have the value /opt/ as is describe it in this tutorial .为了使 Opencv 工作,我必须更改环境变量,使密钥PYTHONPATH具有本教程中描述的值/opt/ Then I have the same problem you describe Unable to import module 'lambda_function': No module named 'boto3' , what is kind of strange as boto3 suppose to be integrate in the path of lambda.然后我遇到了你描述的同样的问题Unable to import module 'lambda_function': No module named 'boto3' ,boto3 假设集成在 lambda 的路径中有什么奇怪的。 In my understanding, is just a conflict with the directories.在我的理解中,只是与目录冲突。

The way it solves for me was changing the value of the environment variables from /opt/ to /opt:/var/runtime .它为我解决的方法是将环境变量的值从/opt/更改为/opt:/var/runtime Hopefully it helps you.希望对你有帮助。

No worries, I can help you with this.不用担心,我可以帮你解决这个问题。 For the first Lambda can't find the file lambda_function.py issue, try prepending src/ to the handler value in the function configuration since your Python scripts are not located in the top-level directory.对于第一个Lambda can't find the file lambda_function.py问题,请尝试在函数配置中将src/添加到处理程序值,因为您的 Python 脚本不在顶级目录中。 For the second issue related to lambda layers, it's true that you messed up the PYTHONPATH environment variable.对于与 lambda 层相关的第二个问题,您确实搞砸了PYTHONPATH环境变量。 If you are using layers, you don't need to overwrite this variable.如果您正在使用图层,则无需覆盖此变量。 Just leave it unchanged.保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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