This lesson teaches the use of dependencies, such as faker.js in your LAMBDA functions. We'll update the lambda code from the lambda and API Gateway lesson to generate random work history and endorsements USI ng The Faker npm module then upload our code to LAMBDA and test.
When you code has dependencies. You cannot just write code in AWS inline editor. You have to zip your code and upload to the AWS.
Index.js:
varFaker = require ('Faker'); Exports.handler= function (Event, context) { varCareer = {}; Career.firstname=Event. FirstName; Career.lastname=Event. LastName; Career.socialmedianame=Getsocialmedianame (); Career.careerhistory= []; for(vari =0; i<5; i++){ varCompany = {}; Company.companyname=Getcompanyname (); Company.desc=Getcompanydesc (); Company.companytitle=GetTitle (); Company.startdate=getDate (); Company.city=getcity (); Company.state=getState (); Company.country=Getcountry (); Career.careerHistory.push (company); } career.endorsements= []; for(vari =0; i< -; i++) {Career.endorsements.push (getendorsement ()); } context.succeed (career);}; function Getsocialmedianame () {returnfaker.internet.userName ();} function Getcompanyname () {returnfaker.company.companyName ();} function Getcompanydesc () {returnfaker.company.bs ();} function GetTitle () {returnFaker.company.bsAdjective () +" "+ faker.company.bsAdjective () +" "+Faker.company.bsNoun ();} function GetDate () {returnfaker.date.past ();} function getcity () {returnfaker.address.city ();} function GetState () {returnfaker.address.state ();} function Getcountry () {returnfaker.address.country ();} function Getendorsement () {returnfaker.company.catchPhrase ();}
Zip index.js and Node_modules folder into a zip folder, then upload to the LAMBDA, save and test. It may report some error, but if your test from Postman, if it works if it fine.
[AWS Lambda] including dependencies in your AWS Lambda function