Deep Dive Into Serverless

@xf3l1x
f3l1x.io

01.10.2021

Server

🌴🌲🎄🌲🏝

Serverless computing is a cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. Pricing is based on the actual amount of resources consumed by an application, rather than on pre-purchased units of capacity. It can be a form of utility computing.

AWS Lambda

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code as a ZIP file or container image, and Lambda automatically and precisely allocates compute execution power and runs your code based on the incoming request or event, for any scale of traffic.

run code without managing servers

lambda allocates execution power

run code with zero administration

exports.handler = function(event, context) { 
    return {
      statusCode: 200,
      body: "Hello PHPlive!"
    }
}
exports.handler = function(event, context) { 
    return {
      statusCode: 200,
      body: "Hello PHPlive!"
    }
}
$ zip function.zip index.js
$ aws lambda \
      update-function-code \
      --function-name my-function \
      --zip-file fileb://function.zip
exports.handler = function(event, context) { 
    return {
      statusCode: 200,
      body: "Hello PHPlive!"
    }
}

AWS CONSOLE

AWS CLI

AWS API GATEWAY

AWS PERMISSIONS

exports.handler = function(event, context) { 
    return {
      statusCode: 200,
      body: "Hello PHPlive!"
    }
}
$ serverless deploy
exports.handler = function(event, context) { 
    return {
      statusCode: 200,
      body: "Hello PHPlive!"
    }
}
service: serverless-http-api
frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: '20201221'

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /
          method: get
$ magic
<?php declare(strict_types = 1);

echo "Hello PHPlive!";

AWS Lambda

AWS Lambda

AWS Lambda

AWS Lambda

USER
PHP LAMBDA

Research

😱

Vercel

ZEIT

$ vercel
<?php declare(strict_types = 1);

echo "Hello PHPlive!";
<?php declare(strict_types = 1);

echo "Hello PHPlive!";

CUSTOM DOMAINS

INTEGRATIONS

GIT (HUB/LAB)

PREVIEW URL

HTTPS

CDN

MULTILANG

FREE TIER

DX / UX

$ vercel

Vercel + 🐘

vercel-php

Runtime API

interface Runtime {
  version: number;
  
  build: (options: BuildOptions) => Promise<BuildResult>;
  
  analyze?: (options: AnalyzeOptions) => Promise<string>;
  
  prepareCache?: (options: PrepareCacheOptions) => Promise<CacheOutputs>;
  
  shouldServe?: (options: ShouldServeOptions) => Promise<boolean>;
  
  startDevServer?: (
    options: StartDevServerOptions
  ) => Promise<StartDevServerResult>;
}

{ CODE }

{ CODE }

RUNTIME

BUILD

site
├── index.html
├── index.php
├── package.json
└── vercel.json
{
    "builds": [{
        "src": "index.php",
        "use": "vercel-php"
    }]
}
$ vercel
builder1.build(ctx)
index.html
dist/main.css
dist/app.js
/api/users
/api/comments

STATIC FILES

LAMBDAS

builderN.build(ctx)

Build Phase

VERCEL SIDE

VERCEL

VERCEL-PHP

build

Build Phase

COMPOSER

INSTALL

COMPOSER

SCRIPTS

CUSTOM

PHP.INI

test

test

test

SETUP

USER FILES

PHP FILES

LAUNCHER

test

test

COLLECT

LAMBDA

LAUNCHER

ENV

test

test

CREATE

CACHE

VERCEL

VERCEL-PHP

build

COMPOSER

INSTALL

COMPOSER

SCRIPTS

CUSTOM

PHP.INI

test

test

test

SETUP

USER FILES

PHP FILES

LAUNCHER

test

test

COLLECT

LAMBDA

LAUNCHER

ENV

test

test

CREATE

CACHE

Build Phase

VERCEL

VERCEL-PHP

build

COMPOSER

INSTALL

COMPOSER

SCRIPTS

CUSTOM

PHP.INI

test

test

test

SETUP

USER FILES

PHP FILES

LAUNCHER

test

test

COLLECT

LAMBDA

LAUNCHER

ENV

test

test

CREATE

CACHE

Build Phase

VERCEL

VERCEL-PHP

build

COMPOSER

INSTALL

COMPOSER

SCRIPTS

CUSTOM

PHP.INI

test

test

test

SETUP

USER FILES

PHP FILES

LAUNCHER

test

test

COLLECT

LAMBDA

LAUNCHER

ENV

test

test

CREATE

CACHE

Build Phase

VERCEL

VERCEL-PHP

build

Runtime Phase

AWS SIDE

CLIENT

VERCEL-PHP

request

Runtime Phase

AWS

LAMBDA

LAUNCHER

PHP SERVER

start
response
request
request
response

CLIENT

VERCEL-PHP

request

Showcases

dag:
  endpoints:
    /api/v1/users:
      generator: alice
      data:
        schema:
          stdClass:
            "user{1..10}":
              name: "<firstName()>"
              surname: "<lastName()>"
              email: "<email()>"
              role: "<jobTitle()>"
              createdAt: "<date_create()>"
              lastLoggedAt: "<date_create()>"

DAG

{
  "user1": {
    "name": "Adah",
    "surname": "Reichel",
    "email": "sstroman@hotmail.com",
    "role": "Skin Care Specialist",
    "createdAt": {
      "date": "2021-09-29 13:33:11.962499",
      "timezone_type": 3,
      "timezone": "UTC"
    },
    "lastLoggedAt": {
      "date": "2021-09-29 13:33:11.962513",
      "timezone_type": 3,
      "timezone": "UTC"
    }
  }
}

Vercel + PHP

USE CASES
  • micros
  • playgrounds
  • generators
  • sandboxes
  • transformers
  • helpers
  • websites

Serverless

+ / -

  • architecture
  • pricing
  • scaling
  • security
  • integrations
  • architecture
  • payload limit
  • memory limit
  • timeout limit
  • concurrency limit

database, SMTP, websockets, 3rd party webservices, ...

Motivation

Motivation

  • new stuff

  • challenge

  • learn sth

  • have fun

Time to selfie 📷

Thank you!

@xf3l1x
f3l1x.io

2021-10-01 - Deep Dive Into Serverless PHP

By Milan Felix Šulc

2021-10-01 - Deep Dive Into Serverless PHP

  • 46
Loading comments...

More from Milan Felix Šulc