DEV Community

Cover image for Create your first serverless function using AWS Lambda and ExpressJs
Peter Tao
Peter Tao

Posted on • Edited on

Create your first serverless function using AWS Lambda and ExpressJs

Converting your current backend to serverless functions might require tons of work but if you are using ExpressJs, there might be an easy way to do so.

In this article, I will walk you through the steps of converting your app to a lambda function in detail.

Say you have an Express app that simply returns a quote of Kanye West from the /data route.


This is how your app.js would look like.

Alt Text

Step One:

Install a useful package. This is the only magic you need for your app.

Alt Text

Step Two:

Modify your app.js like this to use the middleware to get the event object that Lambda receives from the API Gateway.
And do remember to export your app.

Alt Text

Step Three:

Create a new file called lambda.js to wrap your express server as a lambda handler. This will configure a simple proxy API using Amazon API Gateway and integrate it with your Lambda function.

Alt Text

Step Four - Compress the files:

Compress all the files and folders (including node_modules) in the root directory into a .zip file (myfunction.zip in this demo).

Alt Text

Step Five - Create your function:

Login to AWS and go to the Lambda page.


Alt Text


Top right area, create a new function.

Alt Text



Fill out the basic info. Here I name the function as myfunction and pick Node.js as the runtime for obvious reasons.



Alt Text


Create the function. This should redirect you to the function page.


Alt Text

Step Six - Configure your function:

Once you get into the function page, you can now setup your function.


In the Function Code section, upload your .zip file. You may or may not see the actual code in the online editor as it depends on how big is your application.


Alt Text


Edit your Runtime setting section. Change the handler/entry point to lambda.lambdaHandler. The format should be: [name of the file that has your handler].[name of your handler].


Alt Text



Now you have your lambda function set up. However, you can not directly invoke it just yet. You need to configure your API Gateway to action like a middle man between your function and the public internet.

Step Seven - Create API GateWay:

Navigate to the API Gateway page.


Alt Text


Start building a REST API.


Alt Text


Fill out the info.


Alt Text

Step Eight - Create API GateWay Resource:

Click action to create a resource/route.


Alt Text


Name the resource/route the same as your express routes.
Enable CORS.


Alt Text

Step Nine- Create Method for Resource:

Select the resource we just created and hit the action button to create a method for it.


Choose GET method. It should be the same as your /data route.
And of course, you can create multiple methods under the same resource.


Alt Text


Configure the method to connect to your lambda function.
Put the right name and region of your Lambda function. Remember to enable the Lambda integration.


Alt Text

Step Ten - Deploy your API:

Click deploy API under the action button.


Alt Text


Create a new stage and give it a name such as 'prod', 'staging' or 'v1' in this case.


Alt Text


After that, navigate to the stage panel and select the route under v1


Alt Text


On the right-hand side, you can get the invoke link for this route.


Alt Text


Test the route. Now we have a working function.


Alt Text

Usage:

Personally, I don't suggest putting the entire backend logic on lambda, at least not in this way. The reason is that although Lambda function is quite fast nowadays, it still takes a second or two at cold start. But for functionalities like email service, automation pipeline, or image and video manipulation, it would be reasonable to separate them as serverless functions to mitigate the cost.

Conclusion:

Of course, compressing and uploading the file every time you make a code change is silly. However, these just are some baby steps for people who want to try out lambda but have zero patience to deal with AWS (docs or UI). For a better development experience, you might want to use CloudFormation and SAM-cli which will be discussed in the next article.

Top comments (0)