DEV Community

Cover image for ⏱ 10 Minute Tutorial: Creating a Serverless Express Web Server & API

⏱ 10 Minute Tutorial: Creating a Serverless Express Web Server & API

Nader Dabit on January 05, 2020

One of the most popular use cases of serverless functions is deploying and running a web server complete with routing. In this tutorial, I'll show ...
Collapse
 
sorandom1611 profile image
Jason

Thank you for a very interesting article. I just have a quick question regarding this architecture.

There have been concerns surrounding using Lambda to serve as express servers, specifically surrounding the fact that Lambda has a cold start and therefore is unable to serve traffic on the same level as a regular docker container / EC2 instance.

As I am quite new to this architecture, would you mind providing some insight into this issue, as well as why one would look to migrate to this new design?

Collapse
 
matttyler profile image
Matt Tyler • Edited

The python and javascript runtimes provided by AWS for lambda are quite fast, and experience fairly short cold starts in comparison with larger VM based frameworks, such as Java and .NET. If you are using a smaller framework like express or flask, a typical cold start is usually under one second.

Two new features were announced recently to help performance issues. The first is provisioned concurrency, which allows you to set a number of lambda instances that are kept warm. This helps alleviated some of the issues with cold-start penalties and helps with long-tail latency. Secondly, HTTP API's were announced for API Gateway - this mode strips back some of the features of the existing API Gateway service and offers better performance for web frameworks like flask and express.

In most circumstances it's a cost optimization. Re-homing from EC2 or a container will likely result in less management and lower cost - though it does depend on the traffic to the application in question. For example, if you experience spiky traffic load, and/or demand that follows a certain pattern (e.g. significantly fewer users at certain periods), lambda's scale-to-zero model results in lower costs over other alternatives.

There are more advanced ways to architect an application use API Gateway, with things like VTL transformations/service integrations etc, and these offer better cost and performance, but asks for greater investment from a development team to learn and understand. You can often get a quicker return on your investment sticking with existing frameworks that your team is already familiar with.

As such, using light frameworks such as express and flask are viable ways to serve content from lambda and is a good way to start getting to grips with serverless without needing to throw out all your existing knowledge. It's a good example of AWS meeting people where they are.

Details:

aws.amazon.com/blogs/compute/annou...

aws.amazon.com/blogs/aws/new-provi...

Collapse
 
ankurloriya profile image
Ankur Loriya • Edited

amplify function invoke mylambda

{"statusCode":404,"body":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot GET /</pre>\n</body>\n</html>\n","headers":{"x-powered-by":"Express","access-control-allow-origin":"*","access-control-allow-headers":"*","content-security-policy":"default-src 'none'","x-content-type-options":"nosniff","content-type":"text/html; charset=utf-8","content-length":"139","date":"Sat, 30 Jan 2021 07:17:46 GMT","connection":"close"},"isBase64Encoded":false}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
victorioberra profile image
Victorio Berra

I have found this to be cleaner than distributing my endpoints through several unique functions, even using the Serverless Framework for NodeJS. There are some drawbacks, for example, with a dedicated function per endpoint I can scale and warm independently and the Serverless Framework gives me the ability to keep everything in one codebase, configuration as code and share code similar to this approach. More ramblings: Amplify looks like its starting to sprawl.

Collapse
 
pdamra profile image
Philip Damra

I know that "monolith Lambdas" are supposed to be an anti-pattern, but this seems like a great way to facilitate code reuse and help migrate existing codebases to serverless.

Collapse
 
georgewl profile image
George WL

It's weird that the npm module page for the official tooling has such poor documentation and you have to then find the documentation yourself.