Using AWS Lambda with Cognito and API Gateway

2019-08-18 00:21发布

问题:

How to get user in Lambda function? User is authenticated in Cognito and invokes lambda with API Gateway. API Gateway method has AWS_IAM authorizer and checked "Use Lambda Proxy integration" checkbox

回答1:

If you have checked AWS_IAM API Gateway, identity of the end user available to your function. You can access the Identity ID as follows.

exports.handler = function(event, context) {
    var identity = event.requestContext.identity.cognitoIdentityId;
    console.log("clientID = " + identity);

    context.succeed("Your client ID is " + identity);
}

Then using the AWS SDK for Cognito invoking describeIdentity-property method, you should be able to retrieve additional information available for the identity.

var params = {
  IdentityId: 'STRING_VALUE' /* required */
};
cognitoidentity.describeIdentity(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});