Invoking & Testing Serverless Functions Locally with AWS Amplify

nader dabit
Instructornader dabit
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

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.

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 <functionwithserver>. 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.

egghead
egghead
~ 12 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