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?
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!
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? 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.
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.
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);
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With