You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda报错:模块'lambda_function'缺少处理程序'lambda_handler'

解决AWS Lambda中"Handler 'lambda_handler' missing"错误

别担心,这个问题其实是AWS Lambda的基础执行要求没满足,很容易解决!

错误原因

AWS Lambda需要一个指定的入口函数来启动你的代码执行,默认这个函数的名字是lambda_handler。你现在的代码直接写了逻辑,但没有把这些逻辑放到这个入口函数里,所以Lambda不知道从哪里开始运行你的代码,就抛出了这个错误。

修正后的代码

把你的S3检查代码放到lambda_handler函数里就行,示例如下:

import botocore
import boto3

def lambda_handler(event, context):
    # 初始化S3资源
    s3 = boto3.resource('s3')
    bucket_name = 'bucketname'  # 替换成你实际的存储桶名称
    bucket = s3.Bucket(bucket_name)
    exists = True
    
    try:
        # 检查存储桶是否存在
        s3.meta.client.head_bucket(Bucket=bucket_name)
    except botocore.exceptions.ClientError as e:
        error_code = int(e.response['Error']['Code'])
        if error_code == 404:
            exists = False
    
    # 返回执行结果,方便在Lambda控制台查看
    return {
        'statusCode': 200,
        'body': f"存储桶存在状态: {exists}"
    }

几个新手注意点

  • lambda_handler函数必须接受event和context两个参数,这是Lambda的标准要求,哪怕你暂时用不到它们也不能省略
  • 确保你的Lambda执行角色拥有S3:HeadBucket权限,不然代码逻辑没问题也会因为权限不足报错
  • 记得把代码里的bucketname替换成你实际要检查的S3存储桶名称

内容的提问来源于stack exchange,提问作者chpsam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:11:02