Cognito自定义消息触发的Lambda返回InvalidLambdaResponseException

时间:2019-05-08 06:04:15

标签: amazon-web-services amazon-cognito

我创建了一个lambda并将其分配给cognito将UI作为其自定义消息lambda抛出。 这是打字稿中的代码:

export const handler = async (event) => {
    const trigger = event.triggerSource
    const customMessage = cloneDeep(customMessages[trigger])
    if (customMessage) {
        try {
            // inject cognito values to custom message
            const codeParameter = event.request.codeParameter
            const usernameParameter = event.request.usernameParameter
            for (let key in customMessage) {
                let text = customMessage[key]
                if (codeParameter) {
                    customMessage[key] = text.replace(/{{codeParameter}}/g, codeParameter)
                }
                if (usernameParameter) {
                    customMessage[key] = text.replace(/{{usernameParameter}}/g, usernameParameter)
                }
            }
            // load HTML template
            let htmlFile = readFileSync(templateFilePath, { encoding: 'utf8' })
            htmlFile = htmlFile.replace(/(\r\n|\n|\r)/gm, '')
            const template = handlebars.compile(htmlFile)
            const html = template(customMessage)
            event.emailMessage = html
            event.response.emailSubject = customMessage.title
        } catch (err) {
            logger.error(err)
            return event
        }
    }
    return event
}

基本上,它会加载一个html模板文件,并注入代码参数和用户名。

现在我们的注册流程lambda返回的响应是:

{
    "message": "InvalidLambdaResponseException",
    "details": "Unrecognizable lambda output"
}

我事件试图复制粘贴的AWS示例:

exports.handler = (event, context, callback) => {
    //
    if(event.userPoolId === "theSpecialUserPool") {
        // Identify why was this function invoked
        if(event.triggerSource === "CustomMessage_SignUp") {
            // Ensure that your message contains event.request.codeParameter. This is the placeholder for code that will be sent
            event.response.smsMessage = "Welcome to the service. Your confirmation code is " + event.request.codeParameter;
            event.response.emailSubject = "Welcome to the service";
            event.response.emailMessage = "Thank you for signing up. " + event.request.codeParameter + " is your verification code";
        }
        // Create custom message for other events
    }
    // Customize messages for other user pools

    // Return to Amazon Cognito
    callback(null, event);
};

响应是相同的。

有什么建议吗? 谢谢

1 个答案:

答案 0 :(得分:0)

这是我的自定义消息lambda。它在节点8.10上运行。也许您想测试/适应它。我删除了一些其他内容,但应该可以正常工作

exports.handler = function(event, context) {

const cognitoUserPool = 'us-east-1_AAAAAA';
const snsTopicArn = 'arn:aws:sns:us-east-1:9999999999:BBBBBBBBBB';
const baseurl = 'https://company.us-east-1.elasticbeanstalk.com/app';

console.log('Cognito Event:', event);

var AWS = require("aws-sdk");

if(event.userPoolId === cognitoUserPool) {
    if(event.triggerSource === "CustomMessage_SignUp") {
        event.response.emailSubject = "Welcome to Company";
        event.response.emailMessage =  "Hello etc";
        context.done(null, event);
    }
    if(event.triggerSource === "CustomMessage_ResendCode") {
        event.response.emailSubject = "Welcome to Company";
        event.response.emailMessage =  "Some other message etc";
        context.done(null, event);
    }
    if(event.triggerSource === "CustomMessage_ForgotPassword") {
        event.response.emailSubject = "Your password reset";
        event.response.emailMessage =  "Some other message again etc";
        context.done(null, event);
    }
    // Other event types can go here
} else {
    context.done(null, event);
}

};