lambda 中的 AWS Cognito 用户池 AdminCreateUser 不返回错误也不返回数据

Posted

技术标签:

【中文标题】lambda 中的 AWS Cognito 用户池 AdminCreateUser 不返回错误也不返回数据【英文标题】:AWS Cognito User Pool AdminCreateUser in lambda returns no error nor data 【发布时间】:2020-09-19 03:50:24 【问题描述】:

我创建了一个 AWS Lambda 函数来测试新 Cognito 用户池帐户的创建,但没有在用户池中创建帐户,并且控制台日志中没有显示错误。我检查了 AWS Cloud Watch,但 Cloud Watch 也没有报错。

START RequestId: ..... Version: $LATEST
END RequestId: .....
REPORT RequestId: .....  Duration: 80.12 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 84 MB  

我将 Lambda 函数更改为更简单的函数;描述用户池。仍然没有引发错误,并且控制台中没有打印任何用户池信息。我在 describeUserPool 回调函数中添加了一个 console.log 打印,但没有打印。

lambda 函数是使用 AWS 控制台 Lambda 内联编辑器创建的。 Lambda 函数附加了 AmazonESCognitoAccess 策略(此策略具有对 Cognito 用户池的列表/读取/写入访问级别)。

谁能解释我做错了什么? 非常感谢您。

var aws = require('aws-sdk');
aws.config.update(
    accessKeyId: 'access_key_id', 
    secretAccessKey: 'secret_access_key',
    region: 'us-east-1',
    apiVersion: '2016-04-18'
);
var cognito = new aws.CognitoIdentityServiceProvider();
var params = 
    UserPoolId: 'us-region-user-pool-id'
;

exports.handler = async (event) => 
    cognito.describeUserPool(params, function(err, data) 
        console.log('hello from inside function');
        if (err) 
            console.log(err);
         else 
            console.log(data);
        
     );
;

如果我在 AdminCreateUser 中包含不正确的参数,它会报告错误

var aws = require('aws-sdk');
    aws.config.update(
        accessKeyId: 'access_key_id', 
        secretAccessKey: 'secret_access_key',
        region: 'us-east-1',
        apiVersion: '2016-04-18'
    );


var cognito = new aws.CognitoIdentityServiceProvider();
var params = 
    UserPoolId: 'us-region-user-pool-id'
    Username: 'someone',
    TemporaryPassword: '11223344',
    DesiredDeliveryMediums: 'EMAIL',
    MessageAction: 'SUPPRESS',
    UserAttributes: [
    
      Name: 'Email',
      Value: 'someone@example.com'
    ,
    
        Name: 'Family_Name',
        Value: 'One'
    ,
    
        Name: 'Given_Name',
        Value: 'Some'
    ,
    
        Name: 'Phone_Number_verified',
        Value: 'True'
    ,
        
        Name: 'Email_verified',
        Value: 'True'
    

  ],
;

exports.handler = async (event) => 

    cognito.adminCreateUser(params, function(err, data) 
        console.log('hello from inside function');
        if (err) 
            console.log(err);
         else 
            console.log(data);
        
     );
;

我在 Lambda 内联编辑器控制台中收到以下错误消息:

Response:
null

Request ID:
"6e50fdb4-e437-4b84-be7b-3caaeb5b0a98"

Function Logs:
    ncCredentials (/var/runtime/node_modules/aws-sdk/lib/config.js:391:24)
        at Config.getCredentials (/var/runtime/node_modules/aws-sdk/lib/config.js:411:9) 
      code: 'MultipleValidationErrors',
      errors: [
        InvalidParameterType: Expected params.DesiredDeliveryMediums to be an Array
            at ParamValidator.fail (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:50:37)
            at ParamValidator.validateType (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:232:10)
            at ParamValidator.validateList (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:99:14)
            at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:90:21)
            at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14)
            at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)
            at ParamValidator.validate (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:34:10)
            at Request.VALIDATE_PARAMETERS (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:126:42)
            at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
            at callNextListener (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:96:12) 
          code: 'InvalidParameterType',
          time: 2020-05-31T21:59:35.116Z
        ,
        InvalidParameterType: Expected params.UserAttributes[3].Value to be a string
            at ParamValidator.fail (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:50:37)
            at ParamValidator.validateType (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:232:10)
            at ParamValidator.validateString (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:154:32)
            at ParamValidator.validateScalar (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:130:21)
            at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:94:21)
            at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14)
            at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)
            at ParamValidator.validateList (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:103:14)
            at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:90:21)
            at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14) 
          code: 'InvalidParameterType',
          time: 2020-05-31T21:59:35.116Z
        ,
        InvalidParameterType: Expected params.UserAttributes[4].Value to be a string
            at ParamValidator.fail (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:50:37)
            at ParamValidator.validateType (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:232:10)
            at ParamValidator.validateString (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:154:32)
            at ParamValidator.validateScalar (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:130:21)
            at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:94:21)
            at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14)
            at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:88:21)
            at ParamValidator.validateList (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:103:14)
            at ParamValidator.validateMember (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:90:21)
            at ParamValidator.validateStructure (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:75:14) 
          code: 'InvalidParameterType',
          time: 2020-05-31T21:59:35.116Z
        
      ],
      time: 2020-05-31T21:59:35.173Z
    
    END RequestId: 6e50fdb4-e437-4b84-be7b-3caaeb5b0a98
    REPORT RequestId: 6e50fdb4-e437-4b84-be7b-3caaeb5b0a98  Duration: 122.82 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 80 MB  Init Duration: 356.12 ms    

【问题讨论】:

嗨,你能看看它是否被调用。检查 CloudWatch 指标 在 Lambda 控制台中触发了“测试”,Cloud Watch 确认它运行了 successfully. Timestamp Message There are older events to load. Load more. 2020-05-31T16:59:07.766-04:00 START RequestId: 1ea1741e-d9e7-4991-af9d-f72bf73b1133 Version: $LATEST 2020-05-31T16:59:08.202-04:00 END RequestId: 1ea1741e-d9e7-4991-af9d-f72bf73b1133 2020-05-31T16:59:08.202-04:00 REPORT RequestId: 1ea1741e-d9e7-4991-af9d-f72bf73b1133 Duration: 434.49 ms Billed Duration: 500 ms Memory Size: 128 MB Max Memory Used: 83 MB Init Duration: 337.21 ms。但是,Cognito 没有 CloudWatch 指标。 我将函数调用改回 AdminCreateUser。这并没有产生任何 CloudWatch 日志。看起来 lambda 根本没有触发 CognitoIdentifyServiceProvider 函数调用。 【参考方案1】:

找到这个帖子cognito admin not giving error。 @thopaw 的建议解决了我遇到的问题。

通过将其更改为 promise() 基础,它可以按预期工作。 谢谢你们的时间。

如果有人遇到这个,下面是更新的代码。

exports.handler = async (event, context) => 
    console.log('starts');
    var data;
    try 
        const data = await cognito.adminCreateUser(params).promise();
     catch (error) 
        console.log(error);
    
;

【讨论】:

以上是关于lambda 中的 AWS Cognito 用户池 AdminCreateUser 不返回错误也不返回数据的主要内容,如果未能解决你的问题,请参考以下文章

由于错误 AccessDeniedException(Lambda 别名作为 Cognito 触发器),AWS Cognito 用户池引发 PreSignUp 调用失败

AWS API Gateway + Cognito 用户池授权方 + Lambda - 我需要设置哪些 HTTP 标头和权限?

通过 Cognito 生成的授权令牌识别 AWS Lambda 中的用户

AWS Cognito 用户池的事件触发器对象

使用 Lambda 授权方的 AWS Cognito 和 API 网关

AWS cognito 用户迁移池触发器不适用于登录流程