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
This is how your
app.js would look like.
Install a useful package. This is the only magic you need for your app.
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.
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.
Compress all the files and folders (including
node_modules) in the root directory into a
.zip file (
myfunction.zip in this demo).
Fill out the basic info. Here I name the function as
myfunction and pick
Node.js as the runtime for obvious reasons.
Once you get into the function page, you can now setup your function.
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.
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.
Select the resource we just created and hit the action button to create a method for it.
GET method. It should be the same as your
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.
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
On the right-hand side, you can get the invoke link for this route.
Test the route. Now we have a working function.
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.
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.