Skip to content

OASP4FnIntroduction

devonfw-core edited this page Dec 16, 2021 · 20 revisions

OASP4Fn Introduction

Serverless is a framework that allows developers to build auto-scalable applications, pay-per-execution, event-driven apps on AWS Lambda, Microsoft Azure, IBM OpenWhisk and Google Cloud Platform.

OASP4Fn is a npm package full of functionality independent of the goal of the developments made over this framework. It provides many different features following this approach in order to allow the developers use the ones that fit in their needs to build, test and deploy applications in an easy, fast and clean way using the Serverless framework.

Serverless Computing

Serverless computing consists in the following concepts and benefits:

  • Functions as a Service (FaaS).

  • Cloud provider automatically manages starting, stopping and scaling instances for functions.

  • More cost-efficient.

  • The business or person that owns the system does not have to purchase, rent or provision servers or virtual machines for the back-end code to run on.

  • It can be used in conjunction with code written in traditional server style, such as microservices.

  • Functions in FaaS are triggered by event types defined by the provider. For example:

    • HTTP requests.

    • AWS S3 updates.

    • Messages added to a message bus.

Besides the automatic horizontal scaling, the biggest benefit is that you only pay for the compute that you need. Depending on your traffic scale and shape this may be a huge economic win in many projects.

It solves common and inefficient situations like occasional requests, where an application with a few small requests per minute makes the CPU idle most of time, or like inconsistent traffic where the traffic profile of an application is very inconsistent. For example:

Inconsistent Traffic Pattern

In a traditional environment you may need to increase your total hardware capability by a factor of 10 to handle the spikes, even though they only account for less than 4% of total machine up-time.

Installation

OASP4Fn is distributed as a npm package that you can install in your NodeJS application running in a terminal the following command:

$ npm install @oasp/oasp4fn
# or if you have yarn installed
$ yarn add @oasp/oasp4fn

What does OASP4Fn provide on Serverless?

The following picture shows what OASP4Fn offers in order to facilitate Serverless development.

OASP4Fn Adds

Currently, the OASP4Fn architecture defines a series of adapters to help developers to build applications and make use of different cloud providers.

OASP4Fn Available

TypeScript

The Serverless framework is not prepared by default to use TypeScript. OASP4Fn allows the developer to use TypeScript as the programming language in any development, as it provides the types definitions and a pre-configured webpack building system that automates the transpilation of the different handlers.

Infrastructure as Code

The service made with OASP4Fn must follow a specified structure, that along with a configuration file will allow the user to avoid having to configure his service manually.

/handlers
	/Http
		/get
			handler1.ts
			handler2.ts
			…
			handlerN.ts
		/post
			handler1.ts
			handler2.ts
		/put
			…
	/S3
	...
	/{EventName}
		/{TriggerMethod}
			{HandlerName}.ts

The logic of our application must be stored in a folder, called handlers, inside it we will have a folder for each event used to trigger the handler and inside a folder with the name of the method that triggers the handler.

Furthermore of the specified before, in the file oasp4fn.config.js, it is specified the configuration of the events, deployment information and the runtime environment in which the handlers will run.

Annotations

In addition to the configuration file, we can specify to each handler a concrete configuration, related to the event that triggers the handler, using a dummy function that adds or modifies the configuration specified in OASP4Fn configuration file.

// ...
oasp4fn.config({path: 'attachments/{id}'});
export async function getAttachment (event: HttpEvent, context: Context, callback: Function) {
    // ...
}

These annotations will be only interpreted by the framework, so they do not inject or add any kind of functionality to the actual handler.

Cloud Adapters

OASP4Fn also comes with a simple interface that allows the user to have access to different services of cloud providers using adapters.

That interface makes use of this adapters to retrieve data to the user through Promises, and let the user query that retrieved data.

Currently available adapters:

  • AWS

    • AWS DynamoDB

    • AWS S3

    • AWS Cognito

Command Line Interface

OASP4Fn provides a simple command line interface, that using the resources and the information provided by Infrastructure as Code, will help the user generate the proper files to build, deploy and test our application.

Usage: oasp4fn [provider] [options]
   or: fun [provider] [options]

Supported Providers: aws (by default aws)

Options:
  -o, --opts file       file with the options for the yml generation
  -p, --path directory  directory where the handlers are stored
  -e, --express         generates an express app.ts file
  -h, --help            display the help

Next Chapter: Build your OASP4Fn App