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.
Step One:
Install a useful package. This is the only magic you need for your app.
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.
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.
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).
Step Five - Create your function:
Login to AWS and go to the Lambda page.
Top right area, create a new function.
Fill out the basic info. Here I name the function as myfunction
and pick Node.js
as the runtime for obvious reasons.
Create the function. This should redirect you to the function page.
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.
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].
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.
Start building a REST API.
Fill out the info.
Step Eight - Create API GateWay Resource:
Click action to create a resource/route.
Name the resource/route the same as your express routes.
Enable CORS.
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.
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.
Step Ten - Deploy your API:
Click deploy API under the action button.
Create a new stage and give it a name such as 'prod', 'staging' or 'v1' in this case.
After that, navigate to the stage
panel and select the route under v1
On the right-hand side, you can get the invoke link for this route.
Test the route. Now we have a working function.
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)