LoginSignup
5
1

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-04-07

ちょっと素の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にアクセスしてみると、
スクリーンショット 2020-04-06 13.55.43.png
ちゃんと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']),
+   ];
 });

参考

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1