Become a member
to unlock all features

    Invoking & Testing Serverless Functions Locally with AWS Amplify

    nader dabitnader dabit

    In this video you'll learn how to invoke a serverless function locally using the Amplify CLI. You'll learn how to test a basic serverless function as well as one that proxies requests into an Express server with http endpoints.



    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


    Instructor: 0:00 The first thing we'll do is create a new React application by using the create-react-app CLI. Note that this step is optional since we're only going to be working with the Lambda function itself, and not with any client application.

    0:20 Next, we'll initialize a new amplify project by running amplify init. Here, we'll give the project a name, the environment a name, and choose our default text editor as well as a couple of other options.

    0:48 Once our amplify project has been initialized, we'll run amplify add function to create our first Lambda function. For our first function, I'll call this basicfunction since we're going to be working with a very basic Lambda function.

    1:01 For the function template, I'll choose the "Hello, World!" function. When prompted if we'd like to edit the function now, we'll choose yes.

    1:09 The function will be located in the amplify folder in the backend/function/ basicfunction folder. Index.js located in the source directory should be opened up in your text editor. In this function, we'll create a new object called data with the message property set to "Hello, World!".

    1:29 We'll also console.log a message to show that our function has been invoked properly. In context.done, we'll pass in data as a second argument.

    1:55 To invoke the function, we can run amplify function invoke < basicfunction > and choose the index.js as the file and handler as the handler for the function.

    2:09 If everything is working properly, you should see "Successfully invoked the function," as well as the message that we returned from the function.

    2:19 Next, we'll add another function by running amplify function add. This function we'll call functionwithserver since we're going to be running an express server inside of this Lambda function. For the template, we'll choose serverless express function.

    2:37 The file that we're going to be working with is app.js in the source directory within the main function package. I'll first clean up the code by deleting a few comments.

    3:01 Next, I'll create a new endpoint of /people. Within this endpoint, I'll create a new array called people, and then from the function, we'll return the JSON response containing the people array.

    3:39 Now that we've created the function, let's change into the amplify/backend/function/functionwithserver/src directory. Here, you'll notice that we do have our index.js and package.json, but the node modules have not yet been installed.

    3:57 To install the node modules, you can run npm install or yarn. After we've installed our node modules, we'll change back into the root directory.

    4:13 To invoke this function, we'll run amplify function invoke . When we invoke this function, we're actually starting an express server that's located at localhost:3000.

    4:32 To make request against this endpoint, we can now use cURL to curl commands against localhost:3000/people and localhost:3000/items.