1
votes

From my web client I call my AWS Lambda functions directly via:

// Build the logins object for the credentials
var loginKey = "cognito-idp." + config.awsRegion + ".amazonaws.com/" + config.identity.UserPoolId;
var logins = {};
logins[loginKey] = jwtToken;

// Instantiate the credentials.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: config.identity.IdentityPoolId,
    Logins: logins
});

// Must refresh the credentials prior to calling the function.
AWS.config.credentials.refresh(function(err) {
    ... // Error handling excluded

    var lambda = new AWS.Lambda({region: config.awsRegion, apiVersion: '2015-03-31'});
    var params = {
        FunctionName: functionName,
        InvocationType : 'RequestResponse',
        LogType : 'None',
        Payload: JSON.stringify(data)
    };

    lambda.invoke(params, function(err, data){
        ... // Data handling
    });
});

In my Lambda functions I verify there are values for context.identity.cognitoIdentityId and context.identity.cognitoIdentityPoolId as described in the documentation here. The following is a very simplified Lambda function to show exactly the properties I am asking about:

exports.handler = (event, context, callback) => {
    console.log(context.identity.cognitoIdentityId); // Has value
    console.log(context.identity.cognitoIdentityPoolId); // Has value

    callback(null, "success");
};

I figured with this information I could get the Cognito User data, including attributes utilizing this information but I am yet to find a way. This recent SO thread was the closest I have seen, but even they came up with a "work around" type solution.

Does anyone know a way to get the Cognito User data utilizing the Lambda function context.identity data? Thanks!

1

1 Answers

0
votes

Not sure if you solved this but, in your API Gateway, under Integration Request, You must set your Body Mapping Templates, if you use the "default" template for application/json, this will pass along the data to your app.

You'll see a "context" object it creates. Then in your lambda app:

Node Code:

exports.handler = (event, context, callback) => {
let what = event.context;
let who = what['cognito-authentication-provider']; // or match what the template setup

callback(null, "cognito provider: " + who);
 };

Hope this helps!