The serverless gambit: Building ChessMsgs.com on Cloud Run

Interesting read how Greg Wilson built ChessMsgs.com, a website that can track chess games played by sending links to eachother.

Instead of tweeting moves back and forth, players tweet links back and forth, and those links go to a site that renders the current chessboard, allows a new move, and creates a new link to paste back to the opponent. I wanted this to be 100% serverless, meaning that it will scale to zero and have zero maintenance requirements.

The board’s state is represented as a string using the Forsyth–Edwards Notation (FEN)

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

That same FEN is also fed to service that generate static images of the board for use in the meta tags.

The serverless gambit: Building ChessMsgs.com on Cloud Run →
ChessMsgs Source (GitHub) →

Serverless functions with Vercel

Have a static site but looking to add backendy stuff — such as subscribing to a newsletter — to it? Geoffrey Dhuyvetters from madewithlove walks us trough setting up a serverless function with Vercel (formerly known as Zeit):

In this article I’ll walk you through the steps to create serverless functions with Vercel. These functions will provide you with a place to add server side logic like verifying captcha’s, sending emails or liking posts that you can use in your static sites.

Serverless functions with Vercel →

Azure Functions Custom Handlers

Baller addition (in preview) to Azure Functions:

Azure Functions offers first-class support for a limited number of languages. Now in preview, register custom handlers by providing a lightweight HTTP server in any desired language. Use this new capability to extend the language support for your applications, enabling the use of languages or language versions not provided by Azure Functions.

Upon receiving a request the Functions Host will proxy the call to Custom Handler’s Web Server, which can run any language. Its response will then be used as an output binding payload to the target.

Configuration of the Custom Handler happens using a host.json in the root to tell which process to run:

{
    "version": "2.0",
    "httpWorker": {
        "description": {
            "defaultExecutablePath": "node",
            "defaultWorkerPath": "server.js"
        }
    }
}

The contents of your function goes into its own directory. Your code is accompanied by a function.json in which you define the bindings that your function uses. You can bind to queueTrigger for example and output to a queue. HTTP of course is also supported, as per this example:

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get", "post"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

It takes in the Request req and expects a Response res as output.

const express = require("express");
const app = express();

app.use(express.json());

const PORT = process.env.FUNCTIONS_HTTPWORKER_PORT;

const server = app.listen(PORT, "localhost", () => {
  console.log("Your port is ${PORT}");
  const { address: host, port } = server.address();
  console.log("Example app listening at http://${host}:${port}");
});

app.get("/hello", (req, res) => {
  res.json("Hello World!");
});

app.post("/hello", (req, res) => {
  res.json({ value: req.body });
});

What I’m currently missing in the documentation and examples repository though is an example using a Dockerfile to say, for example, run PHP code. On Twitter I found this example by Anthony Chu (PM Azure Functions):

The code itself is not available (as a repo) yet, but by looking at the screenshot you can get an idea of how it all comes together. Will do a follow-up post once example PHP code becomes available 😉

Azure Functions custom handlers →
Sample code for Azure Functions custom handlers (GitHub) →

Going Serverless with Google Cloud Run

Recently I was invited as a speaker to Full Stack Ghent and PHP-WVL. At both events I brought a new talk called “Going Serverless with Google Cloud Run”.

Cloud Run is a fully managed compute platform by Google that automatically scales stateless containers. By abstracting away all infrastructure management, us developers can focus on what matters most: building great applications.

In this talk I’ll show you not only how to deploy PHP/Node applications onto Cloud Run, but also how to create a build pipeline using either Google Cloud Build or Github Actions.

The slides are up on slidr.io, and also embedded below:

Thanks to the organisers for having me, and thanks to the attendees for coming to see me. I hope you all had fun attending this talk. I know I had making it (and whilst bringing it forward) 🙂

💁‍♂️ If you are a conference or meetup organiser, don’t hesitate to contact me to come speak at your event.

Netlify Forms – Manage forms and submissions without any server-side code

Wow, this is an awesome addition to Netlify:

On Netlify, any HTML form can instantly accept submissions—no backend code or infrastructure required. As soon as your form is deployed, you start seeing submissions appear in your Netlify dashboard.

Just add the netlify attribute to any form and everything gets wired up automatically.

Netlify will automatically create some backend code to handle the form submissions. The submissions will be stored and then shown in your control panel. Comes with a free tier of 100 submissions/month.

Netlify Forms →

Video by Phil Hawksworth

Simplify continuous deployment with Project Nebula

Interesting new product (Beta) from the Puppet folks:

Project Nebula automates continuous deployment of applications to multiple cloud-native targets. Our objective is to dramatically simplify continuous deployment of cloud native applications and infrastructure by composing the tools and workflows your developers already use into simple, repeatable deployment workflows.

Project Nebula →
Simplify continuous deployment with Project Nebula →

google/cloud-functions-framework – Google Cloud Functions Framework for PHP

google/cloud-functions-framework is an open source FaaS (Function as a Service) Framework for writing portable PHP functions.

An example function looks like this:

<?php

use Symfony\Component\HttpFoundation\Request;

function helloHttp(Request $request)
{
    return "Hello World from PHP HTTP function!" . PHP_EOL;
}

One can invoke it locally by executing the included router as follows:

export FUNCTION_TARGET=helloHttp
export FUNCTION_SIGNATURE_TYPE=http
export FUNCTION_SOURCE=index.php
php -S localhost:8080 vendor/bin/router.php

Alternatively you can run it in a Docker container (which you can then deploy to Cloud Run):

docker build . \
    -f vendor/google/cloud-functions-framework/examples/hello/Dockerfile \
    -t my-cloud-function

docker run -p 8080:8080 \
    -e FUNCTION_TARGET=helloHttp \
    -e FUNCTION_SIGNATURE_TYPE=http \
    my-cloud-function

Installation per Composer:

composer require google/cloud-functions-framework

google/cloud-functions-framework

⚠️ There is a sample Dockerfile included with the repo, but a first glance tells me it needs some polishing as it uses a full blown GAE_RUNTIME Docker base image and directly installs the composer dependencies into the image itself instead of relying on multi-stage builds.

Bref – Serverless PHP Functions on AWS

Bref comes as a Composer package and helps you deploy PHP applications to AWS and run them on AWS Lambda.

Bref uses the Serverless framework to configure and deploy serverless applications. Being the most popular tool, Serverless comes with a huge community, a lot of examples online and a simple configuration format.

After installing an initing Bref …

composer require bref/bref
vendor/bin/bref init

… all you need to do is wrap your code logic into the Bref-provided lambda fn:

require __DIR__.'/vendor/autoload.php';

lambda(function (array $event) {
    // Do anything you want here
    // For example:
    return 'Hello ' . ($event['name'] ?? 'world');
});

Bref – Serverless PHP made simple →

A Netlify Serverless Function in one Tweet

This is cgi-bin all over again, right?

SymfonyCloud – Serverless Deployment Platform for Symfony

Reduce hosting costs. Stop wasting time and energy finding, building and maintaining your own stack. We cover it all, from PHP to database, search, security and performance.

Enable any service by adding a few lines in a configuration file. SymfonyCloud supports all PHP versions. Each service is deployed in its own custom, lightweight container for optimal performance, stability and flexibility.

Not too sure which provider they deploy to (AWS? GCS? Azure? All of them?), as I couldn’t find it in the docs immediately.

SymfonyCloud →

💁‍♂️ More a Laravel fan? Check out Laravel Vapor.