无法克服Java AWS Lambda Cognito自定义消息触发器的InvalidLambdaResponseException

时间:2018-01-08 08:30:03

标签: java amazon-web-services aws-lambda aws-sdk aws-cognito

每当我在cognito中创建新帐户时尝试使用自定义消息触发器时,我会不断收到InvalidLambdaResponseException并显示“无法识别的lambda输出”消息。 lambda触发器是用java编写的,我很难找到文档或者如何在java中编写一个cognito触发器的例子。我能找到的最好的是here这使我能够弄清楚如何正确格式化输出消息,但仍然抛出异常。我从查看JavaScript示例中了解到,您必须返回收到的事件或具有相同参数格式的对象,我试图这样做无济于事。您将在下面找到我收到的云端监视记录的输入和输出。我做错了什么?

这是原始输入

{version=1, region=us-east-1, userPoolId=*****,
userName= *****, callerContext={awsSdkVersion=aws-sdk-nodejs-2.166.0, 
clientId=*****}, triggerSource=CustomMessage_SignUp,
request={userAttributes={custom:position_group=["All"],
sub=*****, email_verified=false, cognito:user_status=UNCONFIRMED, 
given_name=*****, custom:permissions=, custom:team_id=*****,
email=*****@gmail.com}, codeParameter={####},usernameParameter=null}, 
response={smsMessage=null, emailMessage=null, emailSubject=null}}

这是输出

{
    "version": 1,
    "triggerSource": "CustomMessage_SignUp",
    "region": "us-east-1",
    "userPoolId": "*****",
    "userName": "*****",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-nodejs-2.166.0",
        "clientId": "*****"
    },
    "request": {
        "userAttributes": {
            "userAttributes": {
                "custom:position_group": "[\"All\"]",
                "sub": "*****",
                "email_verified": "false",
                "cognito:user_status": "UNCONFIRMED",
                "given_name": "*****",
                "custom:permissions": "",
                "custom:team_id": "*****",
                "email": "*****@gmail.com"
            },
            "codeParameter": "{####}"
        }
    },
    "response": {
        "smsMessage": "{####} is your verification code.",
        "emailMessage": "TEST {####}",
        "emailSubject": "Verification"
        } 
}

加成 阅读此post后,我尝试将处理程序方法切换到此

public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
        LambdaLogger logger = context.getLogger();
        JsonNode json = mapper.readTree(input);
        logger.log(mapper.writeValueAsString(json));
        ObjectNode jsonWithResponse = (ObjectNode) json;
        jsonWithResponse.with("response").put("smsMessage", "TEST {####}");
        jsonWithResponse.with("response").put("emailMessage", "TEST {####}");
        jsonWithResponse.with("response").put("emailSubject", "TEST subject");
        try (Writer w = new OutputStreamWriter(output, "UTF-8")) {
                w.write(mapper.writeValueAsString(jsonWithResponse));
        }
        logger.log(mapper.writeValueAsString(jsonWithResponse));
    }

之前将其置于云观察中

{
    "version": "1",
    "region": "us-east-1",
    "userPoolId": "*****",
    "userName": "*****",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-nodejs-2.166.0",
        "clientId": "*****"
    },
    "triggerSource": "CustomMessage_SignUp",
    "request": {
        "userAttributes": {
            "custom:position_group": "[\"All\"]",
            "sub": "*****",
            "email_verified": "false",
            "cognito:user_status": "UNCONFIRMED",
            "given_name": "*****",
            "custom:permissions": "",
            "custom:team_id": "*****",
            "email": "*****@gmail.com"
        },
        "codeParameter": "{####}",
        "usernameParameter": null
    },
    "response": {
        "smsMessage": null,
        "emailMessage": null,
        "emailSubject": null
    }
}

,这是在云观察之后

{
    "version": "1",
    "region": "us-east-1",
    "userPoolId": "*****",
    "userName": "*****",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-nodejs-2.166.0",
        "clientId": "*****"
    },
    "triggerSource": "CustomMessage_SignUp",
    "request": {
        "userAttributes": {
            "custom:position_group": "[\"All\"]",
            "sub": "*****",
            "email_verified": "false",
            "cognito:user_status": "UNCONFIRMED",
            "given_name": "*****",
            "custom:permissions": "",
            "custom:team_id": "*****",
            "email": "*****@gmail.com"
        },
        "codeParameter": "{####}",
        "usernameParameter": null
    },
    "response": {
        "smsMessage": "TEST  {####}",
        "emailMessage": "TEST {####}",
        "emailSubject": "TEST subject"
    }
}

1 个答案:

答案 0 :(得分:0)

我的问题是由于我没有在{username}密钥中添加我的电子邮件回复消息(smsMessage和emailMessage)。确保添加密钥{####}{username}解决了这个问题。