• Home
  • Health
  • Software
  • Blog
  • php

    Lambda Notes PHP

    Require at least PHP 7.3, serverless installed.


    composer require bref/bref


    ./vendor/bin/bref init


    Try this out, to see what's available, in index.php:

    <?php
    
    
    require __DIR__ . '/vendor/autoload.php';
    
    
    $context = json_decode($_SERVER['LAMBDA_INVOCATION_CONTEXT'], true);
    
    
    $request = json_decode($_SERVER['LAMBDA_REQUEST_CONTEXT'], true);
    
    
    $a = var_export($request, true);
    $b = var_export($context, true);
    
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>Welcome!</title>
        <link href="https://fonts.googleapis.com/css?family=Dosis:300&display=swap" rel="stylesheet">
        <link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
    </head>
    <body class="flex h-screen">
        <div><?php echo $a . ' ' . $b; ?></div>
        <div class="rounded-full mx-auto self-center relative" style="height: 400px; width: 400px; background: linear-gradient(123.19deg, #266488 3.98%, #258ECB 94.36%)">
            <h1 class="font-light absolute w-full text-center text-blue-200" style="font-family: Dosis; font-size: 45px; top: 35%">Hello there,</h1>
            <div class="w-full relative absolute" style="top: 60%; height: 50%">
                <div class="absolute inset-x-0 bg-white" style="bottom: 0; height: 55%"></div>
                <svg viewBox="0 0 1280 311" fill="none" xmlns="http://www.w3.org/2000/svg"><g clip-path="url(#clip0)"><path d="M1214 177L1110.5 215.5L943.295 108.5L807.5 168.5L666 66.5L581 116L517 49.5L288.5 184L163.5 148L-34.5 264.5V311H1317V25$
            </div>
        </div>
    </body>
    </html>
    


    Then just `sls deploy`.


    It prints out this stuff:


    array ( 'accountId' => '<some number>', 'apiId' => '<some hash id>', 'domainName' => '<some hash>.execute-api.us-east-1.amazonaws.com', 'domainPrefix' => '<some hash>', 'http' => array ( 'method' => 'GET', 'path' => '/this/is/a/test', 'protocol' => 'HTTP/1.1', 'sourceIp' => '1.2.3.4', 'userAgent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36', ), 'requestId' => '<some id>', 'routeKey' => '$default', 'stage' => '$default', 'time' => '24/Oct/2021:05:04:09 +0000', 'timeEpoch' => 1635051849928, ) array ( 'awsRequestId' => '<some id>', 'deadlineMs' => 1635051877977, 'invokedFunctionArn' => 'arn:aws:lambda:us-east-1:<account id>:function:php-bref-dev-php-bref-request', 'traceId' => '<trace id>;Sampled=0', )
    


    More Bref documentation:

    Bref - Serverless PHP made simple