Message AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool not function in aws lambda

I am trying to use the AWS Cognito custom pool in my AWS Lambda function. I saw in the tutorial that you will need to include amazon-cognito-identity.min.js in your code, but I'm not sure how to do this in node js. I use the npm installation for external modules, but I do not think that aws-cognito-identity still exists as a module.

I installed aws-sdk, but the AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool function does not exist in the SDK.

By the way, here is my code in Lambda:

'use strict';
 var AWS= require('aws-sdk');


AWS.config.region = 'ap-northeast-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'ap-northeast-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' // your identity pool id here
});

// Need to provide placeholder keys unless unauthorised user access is enabled for user pool
//AWSCognito.config.update({accessKeyId: 'anything', secretAccessKey: 'anything'})

var poolData = { 
    UserPoolId : 'us-east-1_xxxxxxxxx',
    ClientId : 'xxxxxxxxxxxxxxxxxxxxxxxxx'
};
var userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

module.exports.handler = function(event, context, cb) {

var attributeList = [];
var email = event.email;
var username=event.username;
var password = event.password;

var dataEmail = {
    Name : 'email',
    Value : email
};
var dataPhoneNumber = {
    Name : 'phone_number',
    Value : '+15555555555'
};
var attributeEmail = new AWS.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
var attributePhoneNumber = new AWS.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);

attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber);

userPool.signUp(username, password, attributeList, null, function(err, result){
    if (err) {
        alert(err);
        return;
    }
    username = result.user;
 }
);  return cb(null, username);
};

and here is the error message that I get when testing the lambda function:

{
  "errorMessage": "AWS.CognitoIdentityServiceProvider.CognitoUserPool is not a function",
  "errorType": "TypeError",
  "stackTrace": [
    "Module._compile (module.js:409:26)",
    "Object.Module._extensions..js (module.js:416:10)",
    "Module.load (module.js:343:32)",
    "Function.Module._load (module.js:300:12)",
    "Module.require (module.js:353:17)",
    "require (internal/module.js:12:17)"
  ]
}
+4
source share
1 answer

, :

https://github.com/kndt84/amazon-cognito-identity-js npm:

https://www.npmjs.com/package/amazon-cognito-identity-js-node

. ( 2017 ) General Availability Cognito, AWS ( 2016 ).

npm install amazon-cognito-identity-js-node

var AWS = require('aws-sdk');
var CognitoSDK = require('amazon-cognito-identity-js-node');
AWS.CognitoIdentityServiceProvider.AuthenticationDetails = CognitoSDK.AuthenticationDetails;
AWS.CognitoIdentityServiceProvider.CognitoUserPool = CognitoSDK.CognitoUserPool;
AWS.CognitoIdentityServiceProvider.CognitoUser = CognitoSDK.CognitoUser;

, CognitoUserAttribute.

,

const attributeEmail = new AWS.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);

const attributeEmail = new AWS.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail.Name, dataEmail.Value);
+1

All Articles