Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current user username in AWS Lambda?

I use AWS Lambda + Cognito (User Pool + Federated Identity) + API Gateway. Users authenticate in WEB application with amazon-cognito-identity-js and invokes API with aws-api-gateway-client. API Gateway methods have AWS_IAM authorizer. How to get username (from User Pool) in Lambda function?

like image 816
Ildar Avatar asked Sep 08 '17 23:09

Ildar


People also ask

How do I find my lambda Cognito username?

In order to get the identityId of a Cognito user in a Lambda function we have to call the getId method on the CognitoIdentity class. Let's look at the complete code of a helper method, which retrieves and returns the identityId of a Cognito user. Copied!

How do I get AWS account in lambda?

Configure your Lambda function's execution role to allow the function to assume an IAM role in another AWS account. Modify your cross-account IAM role's trust policy to allow your Lambda function to assume the role. Add the AWS Security Token Service (AWS STS) AssumeRole API call to your Lambda function's code.

What is a lambda user?

What is a lambda user? I'm not the guy who wrote it, but I read "lambda" with the connotation of a lambda function (meaning anonymous closure): a lambda user is a nameless user who only uses the site once.

What is lambda request ID?

You can get the request id and other information from the context object. When Lambda runs your function, it passes a context object to the handler. This object provides methods and properties that provide information about the invocation, function, and execution environment.


1 Answers

You can use event.identity.username

exports.handler = async (event, _, callback) => {
   try {
        console.log('event', event);
        console.log('event.identity.username', event.identity.username);
        const userId = event.identity.username;
        console.log('userId', userId);

        callback(null, true);
   } catch(e) {
       console.log(e);
       callback(e);
   }
};
like image 113
аlex dykyі Avatar answered Sep 29 '22 20:09

аlex dykyі