Create your first serverless function using AWS Lambda and ExpressJs
Tags: serverless, lambda, express, node
Published: 2020-11-21 10:15:08

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.
<br/>

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.
<br/>
This is how your `app.js` would look like.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4tpqj7dw609juc4kyur7.png)

### __Step One:__
Install a useful [package](https://www.npmjs.com/package/aws-serverless-express). This is the only magic you need for your app.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z7b05h1970nal4a11o4c.png)

### __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](https://dev-to-uploads.s3.amazonaws.com/i/40w6t7yztheyqfscbnef.png)

### __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](https://dev-to-uploads.s3.amazonaws.com/i/y6gheeqe6zlfiv5qozwk.png)

### __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](https://dev-to-uploads.s3.amazonaws.com/i/1w0kdokjbuyih6gxjzbn.png)

### __Step Five - Create your function:__
Login to AWS and go to the Lambda page.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mppflnzxku45ej4flxvy.png)
<br/>
Top right area, create a new function.
<br/>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mop3b4zrju07r1834flu.png)

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

<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/248vxa1k51ho4jyhl50n.png)
<br/>
Create the function. This should redirect you to the function page.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x1j8ju3ywfjc5t7orde7.png)


### __Step Six - Configure your function:__

Once you get into the function page, you can now setup your function.
<br/>
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.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gwrqfodjsrbyta5rz4df.png)
<br/>
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]*.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s7msqqgnx5iv0kpg2vt5.png)

<br/>
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.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gywxad51skte6niu03xe.png)
<br/>
Start building a REST API.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w8kvtetjvdovhm7o6hz0.png)
<br/>
Fill out the info.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0rdx3f1oh0mdaxyayv1k.png)

### __Step Eight - Create API GateWay Resource:__

Click action to create a resource/route.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/68p0viqx6lsvezmeaxlw.png)
<br/>
Name the resource/route the same as your express routes.
Enable CORS.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r7lehprjmt7t8q8ssnzz.png)

### __Step Nine- Create Method for Resource:__

Select the resource we just created and hit the action button to create a method for it.
<br/>
Choose `GET` method. It should be the same as your `/data` route.
And of course, you can create multiple methods under the same resource.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sekindgwb51f9ay64p07.png)
<br/>
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.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r71bmqwrz51gvz3ytc3o.png)



### __Step Ten - Deploy your API:__

Click deploy API under the action button.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xioywvm6qdnpdluaq4k0.png)
<br/>
Create a new stage and give it a name such as 'prod', 'staging' or 'v1' in this case.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uaxf7liwcpg4kir4hx9y.png)
<br/>
After that, navigate to the `stage` panel and select the route under `v1`
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/20t3168vto9dal1ej3bf.png)
<br/>
On the right-hand side, you can get the invoke link for this route.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6nwlgyw9og5j1860kqcd.png)
<br/>
Test the route. Now we have a working function.
<br/>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nix8ub6v8x9l0ok539d4.png)

### __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](https://dashbird.io/blog/can-we-solve-serverless-cold-starts/#:~:text=What%20is%20a%20cold%20start,running%20to%20serve%20the%20invocation.). 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.