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

AWS Lambda Python:如何正确通过event对象获取传入的JSON参数

解决AWS Lambda中无法访问event输入字段的问题

你的问题出在Lambda的event参数是Python字典类型,不是类实例,所以不能用event.who这种点号访问方式,得用字典的键访问语法。

修正后的代码有两种写法:

写法1:直接通过键访问

import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from ' + event['who'])
    }

写法2:用get()方法(更安全,避免字段不存在时抛出KeyError)

import json

def lambda_handler(event, context):
    who = event.get('who', 'default value') # 第二个参数是字段不存在时的默认值
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from ' + who)
    }

你的测试事件内容是正确的,不需要修改:

{
    "who": "It is me!"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 06:50:17