Golang对接Cognito触发Lambda提示“Invalid lambda function output”问题排查
问题原因及解决方案
问题出在Lambda函数的返回值类型不符合Cognito PostAuthentication触发器的要求:
Cognito的PostAuthentication属于后置异步触发器,它期望Lambda返回events.CognitoEventUserPoolsPostAuthenticationResponse类型的响应对象,而非原输入的events.CognitoEventUserPoolsPostAuthentication事件对象。你的代码虽然成功完成了数据库插入,但返回的对象格式与Cognito的预期不匹配,因此触发了"Invalid lambda function output"错误。
修改步骤:
- 调整函数返回类型:将函数的返回值从
(events.CognitoEventUserPoolsPostAuthentication, error)改为(events.CognitoEventUserPoolsPostAuthenticationResponse, error) - 修改返回语句:所有return位置都返回空的响应对象(PostAuthentication触发器不需要修改任何输出内容)
修改后的代码示例:
func EjecutoLambda(event events.CognitoEventUserPoolsPostAuthentication) (events.CognitoEventUserPoolsPostAuthenticationResponse, error) { awsgo.InicializoAWS() if !validoParametros() { fmt.Println("Error en los parámetros. debe enviar 'SecretName'") err := errors.New("error en los parametros debe enviar secretname") return events.CognitoEventUserPoolsPostAuthenticationResponse{}, err } var datos models.SignUp for row, att := range event.Request.UserAttributes { switch row { case "email": datos.UserEmail = att fmt.Println("Email = " + datos.UserEmail) case "sub": datos.UserUUID = att fmt.Println("Sub = " + datos.UserUUID) } } err := bd.ReadSecret() if err != nil { return events.CognitoEventUserPoolsPostAuthenticationResponse{}, err } return events.CognitoEventUserPoolsPostAuthenticationResponse{}, bd.SignUp(datos) }
内容的提问来源于stack exchange,提问作者Elba Nanero
相关产品推荐
相关产品推荐

