サーバーレスライブラリbrefを使い、素のPHPでHello world


ちょっと素のPHPが欲しいときがあったら、以下のようにさくっとデプロイできます。

# プロジェクト作成 https://github.com/umihico/bref-demo/commit/0269a62
$ mkdir bref-demo
$ cd bref-demo
$ serverless create --template aws-nodejs --name bref-demo
# brefインストール https://github.com/umihico/bref-demo/commit/2143b4e
$ composer require bref/bref

本命のPHPファイルを追加し、serverless.ymlを設定します。0e8c61d

index.php
+ <?php
+ 
+ require __DIR__ . '/vendor/autoload.php';
+ 
+ lambda(function ($event) {
+     return [
+         'headers' => [
+             'Content-Type' => 'text/html'
+         ],
+         'statusCode' => 200,
+         'body' => '<html><!doctype html><head></head><body><h1>Go Serverless v1.0! Your function executed successfully!</h1></html>',
+     ];
+ });
serverless.yml
 provider:
   name: aws
-  runtime: nodejs12.x
+  runtime: provided
+
+plugins:
+  - ./vendor/bref/bref

 functions:
   hello:
-    handler: handler.hello
-#    The following are a few example events you can configure
-#    NOTE: Please make sure to change your handler code to work with those events
-#    Check the event documentation for details
-#    events:
-#      - http:
-#          path: users/create
-#          method: get
+    handler: index.php
+    timeout: 28
+    layers:
+      - ${bref:layer.php-73-fpm}
+    events:
+      - http: 'ANY /'
+      - http: 'ANY /{proxy+}'
 #      - websocket: $connect

serverless deployで発行されたURLにアクセスしてみると、

ちゃんとHTMLが描写されていますね。jsonを返したい場合はこうです。66df3a


 lambda(function ($event) {
-    return [
-        'headers' => [
-            'Content-Type' => 'text/html'
-        ],
-        'statusCode' => 200,
-        'body' => '<html><!doctype html><head></head><body><h1>Go Serverless v1.0! Your function executed successfully!</h1></html>',
-    ];
+   return [
+       'headers' => [
+           'Content-Type' => "application/json",
+       ],
+       'statusCode' => 200,
+       'body' => json_encode(['json_key' => 'json_value']),
+   ];
 });

参考