For my side project, tracking of favourite TV shows, I created a simple API, that response with the list of data from RSS feed. To make it enjoyable and learn something new on away, I did a serverless infrastructure and wrote some thoughts on what I found out.
If the concept of a serverless application is new to you, in a nutshell, it is a serverless Lambdas (functions) per entry point, stored in a third-party cloud infrastructure provider and executed on-demand β each function stored as a separate package. When you request to a specific endpoint, Lambda function boots up, executes the code and sends back a response.
Pros and Cons
Consider the advantages and disadvantages of choosing serverless architecture over running a server yourself:
-
Pros
- Maintainance - service provider is taking care of the server updates, patches, hardware
- Cost - you pay for what you use; for a number of invocations or computational value
- Security - each function is stored as a separate package per entry-point
- Scaling - scales automatically
-
Cons
- Dependance from 3rd party vendor
- Latency - functions need time to boot up
- Not suitable for long-time running because of price
- Hard to test and debug - challenging to replicate a serverless environment
Choosing provider
As serverless architectures become more popular, more vendors offer their services. To mention popular ones: Amazon AWS Lambda, Google Cloud Function, Cloudflare Workers, Azure Functions.
I looked into AWS and Google services and got lost in the documentation and sign up processes. It looked like too much of a set up for a quick test project.
I've picked Zeit Now Serverless Functions for quick and easy setup, no need for configuration or optimization management.
It supports React, Node.js, Go and many more. You write and push the code with Now, everything else managed automatically, and you receive an entry point to execute your function. Also, it has a free tier, that will probably cover your small project requirements.
Bootstrap project
Zeit Now CLI offers quick templates to bootstrap your projects. If you still don't have Now installed, you can do that by running following command in a terminal:
npm i now -g
To list templates, run:
now init
From the list, select the language or framework that you fancy.
For my project, I use vanilla-functions
template.
Create a Function For an Entry Point
For Now
to create an entry point and function to be valid, it must be placed under /api
directory in the root
of a project and exported as a default.
A simple example of a function that sends back a greeting message as a response would be so:
// api/greet.js
module.exports = (req, res) => {
res.send('Welcome!');
}
If you create a Node.js application, you will have access to some helpers, that is very similar to express.js
:
- request.query
- request.cookies
- request.body
- response.status()
- response.json()
- response.send()
Deployment
Deployment with Now
is a breeze. Just run a command in a terminal from your project root
directory:
now
And see how everything is done for you. After a successful build you will get a deployed application address, similar to this:
https://vanilla-functions-bcb58vyrs.now.sh
To send a request for a greeting message, add /api/greet
suffix to the deployed address:
https://vanilla-functions-bcb58vyrs.now.sh/api/greet
That's it. We created a stupid simple API with a meaningful message.
Conclusion
Serverless infrastructure can be easy to set up, and provider like Zeit Now makes this process friendly for developers. The majority of work is automated and can be done in just a few steps.
Now it's up to you, what's going to happen inside the serverless functions.
If you got interested, see more information about Now Serverless Functions.
Top comments (3)
The only downside I've only came across is using the service with a third party data store. It's so easy to set up now.sh app that you think it's going to be the same for the database π
The easy and not efficient way to solve this is using a file database system, but it's clearly good for small projects.
Hard to test and debug - challenging to replicate a serverless environment
Actually, you can simulate a local serverless enviroment with
now dev
. It's amazing!