Including dependencies in your AWS Lambda function

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

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.

Previous lesson: Use AWS Lambda and API Gateway to return resume data

[00:02] 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.

[00:26] 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.

[01:12] 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.

[01:41] 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.

[02:21] 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.

[02:58] 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.

[03:35] 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.

[04:09] 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.

[04:43] 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.

egghead
egghead
~ 36 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today