Serverless

A brief introduction to serverless.

What is Serverless?

One of the latest buzzwords to hit the web development industry recently has been serverless. The name is actually slightly misleading - because the architecture still uses servers - just not yours.


In short, serverless allows you to invoke functions to run on scaleable infrastructure. You write the function. Someone else runs it.

Benefits

  • Only pay when your code executes
  • Runs code in response to events
  • Run scheduled tasks
  • No server management
  • Automatic scaling
  • Inexpensive
  • Save bandwidth costs if you run in the same provider as your apps

Common Platforms

  • AWS Lambda (Needs a PHP Shim)
  • IBM OpenWhisk (Supports PHP)
  • OpenFaaS (Supports PHP)
  • Azure Functions (Supports PHP)
  • GCP - Cloud Functions (No PHP Support)

Uses of Serverless

Using Serverless makes the most sense for one off tasks that can be extracted into a function.

  • Sending email
  • Transcoding video
  • Sync data between systems

All of the serverless providers give you CLI tools to build and deploy your functions - each provider works in a slightly different way and it is quite time consuming to learn the differences.

This is where serverless.com steps in. It provides an abstraction on top of the providers and gives you an almost uniform workflow regardless of which provider you choose to use.

A couple of sample workflows.

A typical workflow with OpenWhisk

  • Build
  • Deploy
  • Execute

A typical workflow with openfaas

  • Build
  • Push
  • Deploy
  • Execute

OpenFaaS stores functions when they're built as Docker images. Once these are built, they'll need to pushed to a Docker registry such as DockerHub.

An example project with serverless
and IBM OpenWhisk.

$ serverless create -t openwhisk-php --path openwhisk_test_service \
  --name openwhisk_test_service
$ cd openwhisk_test_service && npm install

serverless.yml

The default serverless.yml will need a small addition to make it web accessible.

service: openwhisk_test_service

provider:
  name: openwhisk
  runtime: php

functions:
  hello:
    handler: handler.hello
    annotations:
        web-export: true

plugins:
  - serverless-openwhisk

Then it can be deployed to OpenWhisk with:

$ serverless deploy

Basic PHP OpenWhisk function

A quick demonstration

END.