2

I'm working on creating and lambda function using javascript to authenticate users using the AWS Cognito and Identity pools. But I need to include below JS library references in my lambda function code

aws-cognito-sdk.js
amazon-cognito-identity.min.js
aws-sdk.min.js"
moment.js
sjcl.js
jsbn.js
jsbn2.js

Given below is the part of the code that I used in the handler of the lambda function. How can I include the references to above javascripts inside my lambda function code?

exports.handler = function(event, context, callback) 
{
    // Cognito Identity Pool Id
    AWS.config.region = 'us-east-1';
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'us-east-1:xxxxxxxxxx',
    });

    // Cognito User Pool Id
    AWSCognito.config.region = 'us-east-1';
    AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId: 'us-east-1:xxxxxxxx'
    });

    //...rest of the logic..
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Asanga Dewaguru
  • 1,058
  • 2
  • 16
  • 31

1 Answers1

2

Well, you could make those libraries into Node packages if they aren't already, and then use "require" to include them. The drawback is that you'll have to upload them as a zipped folder from then on, rather than work in their web editor.

master565
  • 805
  • 1
  • 11
  • 27
  • @master565- Even as of now I'm creating a zip folder manually, including all the required files and uploading it to the lambda, therefore it wont be a issue at all. Could you please let me know how to create these node packages? or point me to anywhere there is an example. – Asanga Dewaguru Jun 28 '16 at 15:32
  • Some of them already are (see https://github.com/andyperlitch/jsbn). This answer looks like a good workaround for converting any javascript file into a usable node file as well http://stackoverflow.com/questions/5171213/load-vanilla-javascript-libraries-into-node-js – master565 Jun 28 '16 at 15:45
  • Thanks a lot, I'm working on this. I'll update you on the progress. – Asanga Dewaguru Jun 28 '16 at 16:07
  • Sure thing. Let me know if you have any more questions, I'll try my best to answer them. – master565 Jun 28 '16 at 16:47
  • Just as an update, the web editor now allows you to edit files that are uploaded to a Lambda function as a ZIP package. At least you can edit the main handler file, I haven't had success with any other files. – dmbaughman Dec 28 '17 at 00:08
  • I know, it's amazing. Loving the new interface – master565 Dec 29 '17 at 01:24