This tutorial will show you how to create a serverless blockchain application using Symbol and Netlify Functions.
In particular, we are going to create a blockchain faucet. But first, let’s review what all these concepts mean and how well they fit together.
What is Symbol blockchain?
Symbol is a blockchain platform allowing developers to access advanced blockchain built-in features using a REST API. You can use Symbol to integrate blockchain capabilities in new or existing applications with lightweight code. Read more about Symbol here.
… and serverless?
An application needs to live somewhere to be accessed by others.
Initially, applications were located in servers within the companies facilities. Now, it’s common to hire virtual machines from providers such as Amazon or Google, instead of owning the iron at home. Still, developers have to shift the focus from development to configure the servers, virtual machines, or containers to be able to run their applications.
The serverless architecture provides a higher abstraction for devs, enabling them to execute code in an environment entirely managed by others. In short, there is no time spent setting up the infrastructure, including configuration, availability, load-balancing …
One of the platforms offering Function-as-a-Service (FaaS) is Netlify, which adds a simplistic configuration layer on top of Amazon Lambda.
How everything fits together?
The first clear benefit of running a serverless app is that there is less infrastructure to maintain. Executing serverless functions is usually cheaper than running a highly available and distributed server 24 hours a day, considering that functions are charged per-use.
Imagine a company willing to integrate Symbol blockchain into their existent app written in Ruby. Unfortunately, there is no Symbol SDK for Ruby. Still, to overcome the problem, they could use a lambda function to record transactions on the blockchain.
Overview of the application
To show how can we combine serverless and blockchain apps, we are going to build a serverless faucet for Symbol. Faucets are simple contracts that allow accounts to requests blockchain tokens.
The faucet presented is hosted on GitHub, made available to others thanks to Netlify’s CDN and their service “Functions”.
To run this example, make sure you have Git, NodeJS 12 LTS, and yarn installed. If you have not developed Symbol apps before, I recommend you to complete Symbol’s Getting Started section to create an account and send your first transaction to the network.
Download the boilerplate project
The project symbol-serverless-faucet has all the necessary components to deploy a serverless app.
We are going to test the app first locally, and then deploy it to Netlify. If you want to deploy a custom faucet, jump to “Deploying the app” section.
1. Fork the repository.
2. Clone your repository.
3. Enter the project.
4. Run yarn install.
Developing a lambda function
First, we need to define the serverless function. The file
src/lamba/faucet.js contains the function the faucet is using.
As you can see, a lambda function is developed similarly to any other API. In this case, the function takes as the input
address. Then, we define a transaction, sign it using the faucet’s account, and send it to the network. Finally, the function returns an OK response if the transaction was successfully announced.
All the environment variables are defined in the file
.env, except for one: the private key.
Change the environment variables as desired. Then, open a new terminal and set up the faucet’s private key.
Calling the function
Now that we have defined the function, we can call it. The file
src/index.html defines the form used to make a POST request to the serverless function.
Test the app locally by running
npm run start. Then, open localhost:8080 in a new browser tab.
Deploying the app
Now that we have created and tested the faucet, we can use Netlify to host the app. If you don’t have a Netlify account, sign up here first.
1. Create a new site from Git using this link.
2. Choose to deploy using your GitHub account.
3. Authorize Netlify.
4. Select the repo
5. Open the tab
Advanced build settings and define the following environment variables:
6. Click on the “Deploy Site” button— and we are done!
After the app is deployed, Netlify will show you the URL of the site.
In this tutorial, we only have started to see what we can achieve by combining serverless functions and Symbol blockchain. Nevertheless, we can envision a wide range of promising possibilities. If you create a new serverless app using the boilerplate code shared, do not hesitate to share the link in the comments!