我正在尝试触发 AWS lambda 代码以进行 AWS Cognito 注册
我的行动
import json
import boto3
client = boto3.client('cognito-idp', region_name='eu-central-1')
def lambda_handler(event, context):
print("DEBUG: register a new user")
str = json.dumps(event, indent=2)
print("DEBUG EVENT: ", str) # ERROR - event is empty {}
2 配置的触发器(Cognito -> 用户池属性 -> 预注册 Lambda 触发器)
问题 --
event
函数参数为空。为什么event
是空的?
另一方面,我看到用户已成功创建
我还尝试打印上下文。但没有找到有用的信息
DEBUG EVENT: {}
DEBUG CONTEXT: LambdaContext([aws_request_id=445ea5a1-8c99-4217-8dca-1ec99d01ceb5,log_group_name=/aws/lambda/setCustomAttribsP,log_stream_name=2024/03/08/[$LATEST]119c906a7f894c14bda7315aad7ad087,function_name=setCustomAttribsP,memory_limit_in_mb=128,function_version=$LATEST,invoked_function_arn=arn:aws:lambda:eu-central-1:011431447545:function:setCustomAttribsP,client_context=None,identity=CognitoIdentity([cognito_identity_id=None,cognito_identity_pool_id=None])])
我正在读这个问题AWS Lambda Nodejs 函数中的事件对象为空 但我在我的 AWS 中没有找到代理设置
我可以看到 API Gateway --> API(但我没有看到“资源”按钮)
我怀疑问题与变量名有关
str
,这会覆盖内置的字符串数据类型,这可能有副作用。
试试这个:
import json
import boto3
client = boto3.client('cognito-idp', region_name='eu-central-1')
def lambda_handler(event, context):
print("DEBUG: register a new user")
event_as_str = json.dumps(event, indent=2)
print("DEBUG EVENT: ", event_as_str) # ERROR - event is empty {}
return event
文档告诉您在活动中可以期待哪些信息。
如果不是这样,请包含 CloudWatch 输出,以便我们可以看到错误/堆栈跟踪。
更新:
我添加了
return event
行,因为 Lambda 期望得到响应。
当我自己测试这一点并将用户注册为管理员时,这是我得到的事件:
{
"version": "1",
"region": "eu-central-1",
"userPoolId": "eu-central-1_3ylDrsZAa",
"userName": "37a38ebf-8f59-4623-a364-1693ac23facb",
"callerContext": {
"awsSdkVersion": "aws-sdk-js-2.1545.0",
"clientId": "CLIENT_ID_NOT_APPLICABLE"
},
"triggerSource": "PreSignUp_AdminCreateUser",
"request": {
"userAttributes": {
"email_verified": "true",
"email": "[email protected]"
},
"validationData": null
},
"response": {
"autoConfirmUser": false,
"autoVerifyEmail": false,
"autoVerifyPhone": false
}
}
也许仔细检查 Lambda 函数的事件源 - 是否有其他组件/服务调用它?如果使用来自其他服务或 API 网关的空事件/有效负载调用它,则该事件将为空。对于 Cognito 来说,这似乎不太可能,我认为这是不可能的。