How to get the Cognito Identity id in AWS Lambda

后端 未结 7 1347
[愿得一人]
[愿得一人] 2020-12-28 15:46

How can I get the identity id of the user (logged in by AWS Cognito) that invoked an AWS Lambda function? Do I have to use the SDK on the Lambda function to get the identity

7条回答
  •  青春惊慌失措
    2020-12-28 16:32

    My observation is the following.

    If you call the API Gateway with a signed Request where you actually provide the accesskey, secret and sessionToken which you can extract via (JS SDK):

    AWS.config.credentials = new AWS.CognitoIdentityCredentials(...)
    AWS.config.credentials.get(..)
    

    And assumed that your lambda is called from API-Gateway via LAMBDA_PROXY and Authorizer AWS_IAM. You can only access user stuff in lambda with:

    exports.create = function (event, context) {
       secdata = event.requestContext.identity.cognitoAuthenticationProvider;
    }
    

    Then you will get, apart from other stuff, the "sub" of the cognito UserPool User. So if you really want to know more about the user, it seems you need to ask AWS again via SDK call.

提交回复
热议问题