This lesson teaches you how to use dependencies, such as faker.js in your Lamdba functions. We will update the lambda code from the Lambda and API Gateway lesson to generate random work history and endorsements using the faker npm module then upload our code to Lambda and test.
In the last lesson we created an API that allowed us to submit a name and get a resume returned for a specific person. Now the resume details that we had were just statically included in our function, in our lambda function, so let's make that a little bit more dynamic. We're going to use the faker module to dynamically generate some fake resume data.
Our first step is going to be to install the faker module, so we'll do npm install faker, and include it in our application via a require statement, and then we're going to export a function named handler just like we did in the AWS console. We're going to grab our first name again from the event object, same thing for the last name, and now we want to create a social media name and it's going to call a function called getSocialMediaName. Let's create that function real quick.
All it's going to do is return the result of faker.Internet.UserName, and when you use the faker module, it has these different objects that it knows how to create fake data for, user name is one of them, and so we're just going to return that as our social meda name. If you go to the faker website you can actually scroll through here and see the different methods that are available.
For example, we just used the Internet.UserName method, and we're also throughout the course of this lesson going to use companyName and some of the address methods as well. We need to include our work history, and that's going to be an array. Then I'm going to use a static loop here to generate five companies to include on this resume. So we've generated some fake data, we have a company name, a description, the title of the position, the start date, the city, state, and country that the job was in.
The last thing we need to do is push this company object onto our work history array, and we'll do that with resume.workHistory.push and then use the company object. So I also included a bunch of functions here to generate the fake data, and those haven't been created yet, but I'm going to create them just like we did the getSocialMediaName function. So for the first one getCompanyName, it's just going to return faker.company.companyName.
I'm just going to continue doing that for the rest of the functions we've defined. I also want to include some endorsements on the resume, and we're going to do a static loops of 20 to generate 20 endorsements and we'll just push those onto the array. So that's going to complete our resume, so our last step will be to call the context.succeed method and then pass in the resume object itself to return that to the client.
Now this looks very similar to what we did in the first lesson, except that we typed this into an editor this time instead of typing it into the built in editor in the lambda console, and the reason we did that is because we included an external module, the faker module, and that brings along some extra files with it. So anytime you have dependencies in your lambda function, you can't use the built-in editor. So we're going to actually create a zip file and upload that zip file that contains all of our code.
To zip it up I'm going to jump into a terminal and I'm going to type zip -r resume which will be the name of our zip, and a * to include all the files. You can use the file selector and select our zip file we created and hit save and test and that will upload the file and test it for us. So it succeeded, it returned the name, it gave me a social media name, generated a list of five companies, and here are our 20 buzzword friendly endorsements.
So now this is inside of lambda, let's jump into the Postman REST client and see how it looks from there. We want to do a post request, we can submit that, and in our response the name that we provided was returned as well as social media name, again the work history, and 20 randomly generated endorsements.