How To Build Serverless Applications Using The Serverless Framework And Fauna.
In a nutshell, we'll look at the various components and tools required to construct serverless applications using Fauna and the Serverless framework. We'll go over the many components that make up a serverless application.
We'll start with the fundamental idea of serverless computing, or serverless apps, to better understand how they vary from traditional applications. After that, talk about how serverless applications use Fauna and Serverless Framework.
Introduction
For developers unfamiliar with the concept, the first thing that comes to mind when serverless is spoken is that it doesn't need servers. I'm happy to inform you that servers are still necessary for the proper operation of your program. Here's a brief explanation of serverless:
A technique for offering backend services on an as-needed basis is serverless computing. The servers are still in use, but the developer(s) or business that uses a serverless vendor to provide backend services is charged based on compute — use, rather than a set quantity of bandwidth or number of servers.
Because of this, the serverless provider may offer the services required by users to build and deploy applications without having to worry about the underlying infrastructure.
In this lesson, we'll talk about serverless as function-as-a-service, a serverless backend service that enables developers to create and modify modular bits of code on the fly. These pieces of code would then be run in response to certain application events. The strategy is to write our code in the form of functions that would deal with various requests or tasks within the application. Therefore, we will call our functions in the form of an event when we deploy them.
This enables serverless applications to scale dynamically in response to requests and eliminates the need for capacity planning or application provisioning.
This brings us to a problem that we frequently run into when developing serverless applications: the most widely used cloud databases do not currently offer this level of elasticity, so you are forced to pay for storage that you don't utilize. Additionally, there aren't enough joins, indexes, authentication, or other processes to create a rich application. Fauna now enters the picture.
Fauna and Serverless Framework Overview
Without the typical configuration of customized settings,fauna.com builds web apps using an already-existing infrastructure. A worldwide distributed database that needs no provisioning is called fauna.com as Serverless Cloud. We only pay for what we use within our serverless application because the capacity for our apps is metered and also accessible on demand. Fauna is a transactional database that is adaptable, developer-friendly, and supplied as a safe, scalable cloud API with native GraphQL. We have good reason to never worry about database provisioning, scaling, sharing, replication, or consistency because to this flexibility.
Open source software called Serverless Framework creates, assembles, and packages code for serverless deployment before deploying the package to the cloud. By utilizing the serverless.com/framework/docs, we are able to design, deploy, troubleshoot, and secure our serverless apps with significantly less overhead and expense. A hosted dashboard and an open source CLI make up the Serverless Framework. They enable you to manage the entire serverless application lifecycle.
Enough talk; let's get started investigating the various components of serverless applications using Fauna and Serverless Framework.
Prerequisite
- Fauna basics are necessary but not required.
- have nodejs.org/en installed on our local machine at version 12.0 or higher.
- Knowledge with the Serverless framework is necessary but not required.
Start using the Serverless Framework.
To easily set up and provision serverless functions across several cloud providers, our initial strategy would be to install the serverless.com/framework/docs globally on our local system.
Serverless framework can be installed using npm or as a standalone binary. We'll discuss both installs for reasons of choice.
Install the binary as a standalone:
MacOS/Linux
curl -o- -L https://slss.io/install | bash
Simply enter the command in your terminal to install the most recent version using the aforementioned command.
curl -o- -L https://slss.io/install | VERSION=2.21.1 bash
The command shown here demonstrates how to install a particular version by setting the VERSION variable.
Window
We'll employ chocolatey.org, one of Windows' package managers.
choco install serverless
Install through npm:
The serverless CLI is installed using the aforementioned command and the npm node package manager.
Once the installation is complete, it will search the standard environment variables for AWS keys.
Get started with Fauna
To get started with Fauna, we'll first sign in to our existing account or create a new one as a new user by using our email address or our Github username. Create a new account by doing so here dashboard.fauna.com/accounts/register?utm_s... The dashboard panel will greet us after we create or in into the account. If you enjoy the shell environment, there is also github.com/fauna/fauna-shell. Through the terminal, it is simple to create and/or modify resources on Fauna.
The command to use fauna shell is as follows:
npm install --global fauna-shell
fauna cloud-login
Now that we have our data ready, we can experiment with queries in the Fauna dashboard. When you open the dashboard, you'll see something similar to this:
We may start creating our Fauna now that we have logged in or created our accounts. In order to construct the new fauna database using Fauna services, we'll go through the basic steps listed below. We begin by giving our database a name. We will refer to our database in this tutorial as debt tracker.
The next step is to establish a new data collection from the Fauna dashboard after the database has been built. Click the NEW COLLECTION button after navigating to the Collection tab on the side menu to start a new collection.
Then, we'll proceed to give our collection whatever name works best. Debt tracker will also be used here.
The following step is to retrieve our API key from the Security sidebar link. To create the API key we'd need for our program, click the "new key" button.
The API key for the database we just generated will be shown on the following screen. Take note to immediately copy and store the API key. Once you switch to another page from the current page, the key is gone.
Use Our Serverless Application to Get Started
We will now go over the many components involved in creating a serverless application so that you can quickly create a complete application using the straightforward code units shown in this article.
Let's begin by cloning the serverless-crud project, which will provide us with sample code that will make it simple for us to conduct CRUD operations with our application.
serverless install --url https://github.com/faunadb/serverless-crud
Please rename some of the files to reflect the name of the application you are creating as a side note. The base name will change from todos to debt tracker inside this tutorial. After the cloning is complete, we'll change directories, cd into the service and the cloned folder, and then install all necessary dependencies inside the folder.
cd serverless-crud
npm install
After all the required packages have been installed, open the serverless.yml file, scroll to the MY FAUNADB SERVER SECRET field, and change the value with the API key that we previously copied and saved. Apply the same steps to the package.json file as you did earlier.
The serverless.yml file, which is only a configuration file unique to the Serverless framework, contains information about the utilities that will be examined in the near future. The functions field and all of its values will be the main focus of our attention. This includes all the details of how our serverless functionalities are set up to work:
functions:
create:
handler: handler.create
events:
- http:
path: debt_tracker
method: post
cors: true
readAll:
handler: handler.readAll
events:
- http:
path: debt_tracker
method: get
cors: true
readOne:
handler: handler.readOne
events:
- http:
path: debt_tracker/{id}
method: get
cors: true
update:
handler: handler.update
events:
- http:
path: debt_tracker/{id}
method: put
cors: true
delete:
handler: handler.delete
events:
- http:
path: debt_tracker/{id}
method: delete
cors: true
The code above simply says to seek for a folder called functions that has a file named handler.js. This file contains defined utilities and functions that are exported for particular tasks. Five serverless functions that will execute the CRUD operation on the database we put up earlier in this article can be found in the code above. So let's study the functions by navigating to the handler.js file.
Our serverless functions, which were defined in the same working directory as handler.js, are imported within the handler.js file to make various tailored calls to the database.
("use strict");
const debt_trackerCreate = require("./todos-create.js");
const debt_trackerReadAll = require("./todos-read-all.js");
const debt_trackerReadOne = require("./todos-read-one.js");
const debt_trackerUpdate = require("./todos-update.js");
const debt_trackerDelete = require("./todos-delete.js");
module.exports.create = (event, context, callback) => {
debt_trackerCreate(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
module.exports.readAll = (event, context, callback) => {
debt_trackerReadAll(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
module.exports.readOne = (event, context, callback) => {
debt_trackerReadOne(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
module.exports.update = (event, context, callback) => {
debt_trackerUpdate(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
module.exports.delete = (event, context, callback) => {
debt_trackerDelete(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
The first function will accept an object data and automatically build a debt record for us in our database. The second function accesses the database and retrieves every debt document that we've produced. The third, fourth, and fifth functions, on the other hand, will retrieve, modify, or remove a specific debt document using the ID that has been provided.
The next step would be to deploy our application to any cloud provider of your choosing now that we have made all of these available. simply make API requests against the application's available endpoints.
Run the following command to create a debt document:
curl -X POST https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker --data '{ "debt" : "Owing the UN a visit" }'
Run the command: to read all the debt documentation.
curl https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker
Run the command with the supplied ID to read one of the debt documents.
curl https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker/<id>
Run the command with the supplied ID to update one of the debt documents:
curl -X PUT https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker/<id> --data '{ "debt" : "Owing my travel agency" }'
Run the following command with the supplied ID to remove one of the debt documents:
curl -X DELETE https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker/<id>
This includes examples of our hypothetical requests for the endpoints.
Conclusion
I'd like to congratulate you on completing this tutorial. The decision of which tools/framework to employ with any cloud services provider will always fall to Fauna and the serverless.com framework because we built this project and look forward to constructing new applications using a serverless architecture. The numerous advantages that we can always anticipate, such as the illustration of serverless code dynamically provisioning resources using FauanDB's multi-tenant QoS features and the simple integration of Fauna with other serverless components.