February 4, 2019

Serverless Functions, Command-Line Interface, and Server-Side Builds in 8base

8base
@8base

Remember the days when expensive servers were stacked in a room with special-purpose air-conditioning in a company’s offices? This problem was alleviated with the emergence of data centers and later virtualization. Then cloud services like Amazon Web Services came about and eliminated the need for companies to purchase physical servers completely. We are now witnessing the next evolution of computing capabilities with the advent of serverless computing.

The Serverless movement is rapidly taking over the world of business computing. Serverless is an architecture that allocates compute capacity on demand for each request, rather than maintaining idle servers or containers while waiting for traffic to arrive. Amazon Web Services (AWS) pioneered serverless with its Lambda product and now all major cloud providers are investing heavily in offering similar products. For businesses, this is an optimal architecture that truly makes utilizing computing capacity feel like utilizing electricity from an electrical utility — they only use the amount they need at any time.

Serverless is notable in the evolution of cloud computing, completing the logical separation between a computer program and its execution infrastructure. From a developer perspective, this means that you can simply deploy your code and the cloud platform takes care of provisioning resources for its execution — whether it is a single request or a thousand requests. There is no need to maintain and pay for idle servers or configure auto-scaling procedures.

However, transitioning to serverless is not always simple. Not only does it change the way you structure and write code because of the stateless nature of serverless functions (each request is executed in an independent copy of the program), but serverless also requires you to rethink continuous integration, as well as pooling of database connections and caching, amongst other things. 8base was developed to be fully serverless, therefore our own development team had to deal with many of those same challenges.

Serverless Extensibility

In addition to 8basebeing serverless, we wanted all applications built on 8base to also be serverless. We also wanted to make the experience simple for developers. So, 8base provides tools for developers to deploy and execute logic in a serverless environment. Developers can author custom backend logic in JavaScript or TypeScript and deploy it using the 8base’ CLI(Command Line Interface). This logic can be exposed through GraphQL or REST, triggered in response to data events or executed on schedule.

Deployment, provisioning, and execution of functions all happen automatically inside the 8base platform — eliminating any worries regarding initial configuration or ongoing scaling of the backend workloads.

Using the 8base CLI, you can deploy custom serverless functions in seconds that become available instantly. The following function types are supported:

  • Resolvers: Use this to add arbitrary queries and mutations to the GraphQL API. You can make the API go beyond CRUD (Create, Read, Update, and Delete) and even “stitch” other GraphQL APIs into yours.
  • Triggers: You can run custom logic in response to data events. For example, send a welcome email after a new employee record is created or validate an order before it is created or updated.
  • Webhooks: Need to expose a traditional REST endpoint? 8base has you covered with webhooks.
  • Scheduled Tasks: Run these at any time and frequency based on a schedule.

The logic execution is fully serverless, creating and destroying compute resources in real-time based on demand, which means you don’t have to pay for idle servers when traffic is low or add more servers when it is high.

Building Serverless Functions in 8base

The structure of an 8base Logic project is simple. The configuration of your application is defined in 8base.yml. There you can configure multiple serverless functions, their type, and their path to the source code. You can read more in 8base.yml documentation.

Here is an example of 8base.yml with all types of functions:

{% code-block language="yaml" %}
functions:
 resolverExample:
   handler:
     code: src/resolverFunc.ts
   type: resolver
   schema: src/resolverFunc.graphql

 triggerBefore:
   handler:
     code: src/triggerBefore.ts
   type: trigger.before
   operation: TableName.create

  triggerAfter:
   handler:
     code: src/triggerAfter.ts
   type: trigger.after
   operation: TableName.create

 webhookExample:
   handler:
     code: src/webhookFunc.ts
   type: webhook
   path: webhook_url  
   method: POST

 taskExample:
   handler:
     code: src/taskFunc.ts
   type: task
   schedule: 'rate(1 minute)'
{% code-block-end %}

Running {% code-line %}8base deploy{% code-line-end %} in a directory with 8base.yml deploys the custom logic to the cloud and makes it ready for use.

Note on server-side builds and local development

One of the challenges with serverless deployment is that any binary dependencies should be compiled for the same platform that functions run on. For example, AWS Lambda uses a flavor of CentOS Linux for its containers. This means that binary dependencies should be compiled on CentOS. If you use Mac OS for development and compile (npm install) binary dependencies on Mac OS, they will not work on Lambda. 8base solves this problem using server-side builds.

When you run {% code-line %}8base deploy{% code-line-end %}  the CLI uploads your code to the cloud and runs server-side build that downloads and compiles dependencies and then packages the bundle for deployment into the serverless execution environment. This not only accelerates deployment speed (~10 seconds for simple scripts) but makes it possible to have binary dependencies in your applications as well as the capability to use a non-Linux OS for development.

The 8base CLI provides tools to simplify the local development and debugging experience. You can execute each function remotely and locally, as well as view remote logs. You’ll find more information here.

Try it out

Now that you have learned more about 8base Serverless you can try deploying your own functions for free. Learn how to install the 8base CLI and deploy serverless functions in our documentation.

Ready to try 8base?

We're excited about helping you achieve amazing results.