我正在尝试使用 Python 3.6 创建简单的 Lambda 函数。
该函数应该在请求查询字符串参数中获取一个 userId(我在 DynamoDB 中的主键),如果数据库中存在项目则返回 200,这是我的 lambda 函数
import boto3
import os
from boto3.dynamodb.conditions import Key, Attr
def lambda_handler(event, context):
userId = event["userId"]
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['Customers'])
items = table.query(
KeyConditionExpression=Key('userId').eq(userId)
)
return items["Items"]
当我在 Lambda 接口中进行测试时,它可以工作并返回正确的用户,但是,当从 Postman 尝试或使用 API Gateway 时,它返回以下错误
{
"errorMessage": "'userId'",
"errorType": "KeyError",
"stackTrace": [
[
"/var/task/index.py",
7,
"lambda_handler",
"userId = event["userId"]"
]
]
}
- 我在这里错过了什么?
- 努力理解“事件”,文档说明它是一个 python 字典,但我如何打印它的结果并在从 Postman 或 API 网关调用时实际调试 lambda?
原文由 JamZ 发布,翻译遵循 CC BY-SA 4.0 许可协议
您正在使用
event["userId"]
,这意味着例如发送请求负载然后上面的代码有效,假设你想发送 userId 作为路径参数
然后你可以访问lambda函数
最好添加打印语句 print(event) 并检查 cloudwatch 日志中的日志