1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Use external dependencies in an AWS Lambda function

    Tomasz ŁakomyTomasz Łakomy
    awsAWS

    Web developers tend to use external dependencies (e.g. npm packages) quite often, and lambda functions are not exception - there's no need to reinvent the wheel if we can use a package created by someone else.

    In this lesson we're going to learn how to create a Lambda Function Package in order to create and deploy an AWS Lambda function which uses an external dependency - axios

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 0:00 We have a Lambda function with an API gateway attached to it. I can call this function by going to this API endpoint. What we would like to do is to run this code in our Lambda function.

    0:09 The problem is this code is using an external dependency. I cannot just go ahead and paste this into the AWS console, because it won't work. We need to be able to provide axios as a dependency to our Lambda function.

    0:22 In order to do that, we have to use Lambda function packages. In AWS Lambda, not only we can edit the code in this editor, but we can also upload a zip file containing our code as well as all the dependencies. To create a Lambda function package, we have to zip both the index.js as well as node modules.

    0:38 In order to do that, I'm going to use the zip command, but you can use whatever you want in order to create a zip file. What matter is that we have to include all the code and dependencies inside. I'm going to create this new zip file called lambda_package. Now, let's go ahead and upload it to AWS. Click over here, upload the package, and click save.

    0:56 Now we can see the code that we've just uploaded in this editor over here. There are also all those node modules, including axios, that we are using in order to get a random cat fact. Now if I go over here to our API, and refresh it, I'm going to get a random cat fact.