'How to return CognitoIdentityCredentials from lambda

I am trying to send the Cognito identity Credentials back to the client but am struggling to get the data after it has been initialized. Currently it is returning undefined and when I print the config credentials it is clear that it is printing before initialization.

const AWS = require('aws-sdk');

exports.handler = function(event, context, callback) {

    AWS.config.region = 'us-east-1';
    var token = event.authorizationToken;

    // Configure credentials
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: IdP,
        Logins: { 'example.com': token }
    });

    // Obtain credentials
    AWS.config.credentials.get(function(){
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
            identityId: AWS.config.credentials.identityId
        });
        console.log(AWS.config.credentials)
        callback(null, {
          statusCode: 200,
          body: JSON.stringify(AWS.config.credentials.CognitoIdentityCredentials)
        });
    });
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source