【问题标题】:Missing handler error in AWS LambdaAWS Lambda 中缺少处理程序错误
【发布时间】:2022-05-10 00:50:23
【问题描述】:
对于基本问题,我深表歉意。我对 AWS 和 Python 完全陌生。我正在尝试执行https://boto3.readthedocs.io/en/latest/guide/migrations3.html#accessing-a-bucket 中给出的示例代码,但遇到了错误。
import botocore
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucketname')
exists = True
try:
s3.meta.client.head_bucket(Bucket='bucketname')
except botocore.exceptions.ClientError as e:
# If a client error is thrown, then check that it was a 404 error.
# If it was a 404 error, then the bucket does not exist.
error_code = int(e.response['Error']['Code'])
if error_code == 404:
exists = False
日志中的错误是
“errorMessage”:“模块上缺少处理程序'lambda_handler'
'lambda_function'"
【问题讨论】:
标签:
python
amazon-web-services
aws-lambda
【解决方案1】:
您需要在代码中定义一个函数。代码缺少名为lambda_handler 的函数。您的代码应如下所示:
import botocore
import boto3
def lambda_handler(event, context):
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucketname')
exists = True
try:
s3.meta.client.head_bucket(Bucket='bucketname')
except botocore.exceptions.ClientError as e:
# If a client error is thrown, then check that it was a 404 error.
# If it was a 404 error, then the bucket does not exist.
error_code = int(e.response['Error']['Code'])
if error_code == 404:
exists = False
【解决方案2】:
将您的代码移动到 Python 函数中。您可以给它起任何名称,但这将成为您的处理程序。转到 lambda 函数基本设置并将默认处理程序更改为 <yourfilename>_<yourfunctionname>。默认情况下,当你创建一个 lambda 函数时,文件名是lambda_function_name.py(你可以改变它),你的处理方法是lambda_handler(你可以改变它),所以入口点是lambda_function_name.lambda_handler。
【解决方案3】:
Kishna_mee2004 是对的,你需要定义 lambda_handler 没有这个它永远不会工作,但如果你得到以下错误:
模块“jobdata_rdcmedia_s3_Etl_job_scheduler_lambda”上缺少处理程序“py”:“模块”对象没有属性“py”
然后您需要检查处理程序信息是否提到了lambda_function_name.lambda_handler。