LoginSignup
7
6

More than 3 years have passed since last update.

Laravelのサーバレス用ライブラリbrefを使い、lambdaでhello world

Last updated at Posted at 2020-03-21

2021/02/24現在はコンテナでlambdaにできるようになってる他、その他多くの手順もベターなものがあり、リライトしました。
当記事ではなく、こちらをご参照ください。
https://qiita.com/umihico/items/514cf792d30bf3706ef5

プロジェクトを作ってから少し手を加えるだけで、Laravelのサーバレス化ができました。
AWS上のデプロイはServerless Frameworkが全てやってくれます。

本家のドキュメントはこちら
Githubはこちら

composer create-project --prefer-dist laravel/laravel laravel-demo #プロジェクト作成
cd laravel-demo
composer require bref/bref #肝のbrefインストール

以下の編集を加えます。b508b15

.env
- SESSION_DRIVER=file
+ SESSION_DRIVER=array
+ VIEW_COMPILED_PATH=/tmp/storage/framework/views
config/logging.php
         'stack' => [
             'driver' => 'stack',
-            'channels' => ['single'],
+            'channels' => ['stderr'],
             'ignore_exceptions' => false,
         ],
app/Providers/AppServiceProvider.php
     public function boot()
     {
-        //
+        if (!is_dir(config('view.compiled'))) {
+            mkdir(config('view.compiled'), 0755, true);
+        }
     }
 }

最後にserverless.ymlを追加します。007fb31

serverless.yml
service: laravel-demo

provider:
  name: aws
  region: ap-northeast-1
  runtime: provided

plugins:
  - ./vendor/bref/bref

package:
  exclude:
    - node_modules/**
    - public/storage
    - storage/**
    - tests/**

functions:
  website:
    handler: public/index.php
    timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
    layers:
      - ${bref:layer.php-73-fpm}
    events:
      - http: 'ANY /'
      - http: 'ANY /{proxy+}'
#  artisan:
#    handler: artisan
#    timeout: 120 # in seconds
#    layers:
#      - ${bref:layer.php-73} # PHP
#      - ${bref:layer.console} # The "console" layer

本家のartisanコマンド用関数ですが、私はローカルでしか実行しないのでコメントアウトしています。

デプロイコマンドはphp artisan config:clearsls deployのセットです。
以下のアウトプットになりました。

$ php artisan config:clear
Configuration cache cleared!

$ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service laravel-demo.zip file to S3 (14.19 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.................................
Serverless: Stack update finished...
Service Information
service: laravel-demo
stage: dev
region: ap-northeast-1
stack: laravel-demo-dev
resources: 12
api keys:
  None
endpoints:
  ANY - https://td3rzowchc.execute-api.ap-northeast-1.amazonaws.com/dev
  ANY - https://td3rzowchc.execute-api.ap-northeast-1.amazonaws.com/dev/{proxy+}
functions:
  website: laravel-demo-dev-website
layers:
  None
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.


さっそく https://td3rzowchc.execute-api.ap-northeast-1.amazonaws.com/dev にアクセスしてみましょう。
スクリーンショット 2020-03-21 8.50.03.png

以上です。以下の課題に対しても記事投稿予定です。

  1. URLにあるステージのパスが外せないことによる不具合が多いので、カスタムドメインを導入する
  2. Basic認証を付与する
  3. public配下が参照されず、jsとcssが参照できない。
  4. slsコマンドと同期して.envを環境毎に使い分けたい。
  5. セッション含めDB接続したいがサーバレスはコネクションプール問題がある。
7
6
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
7
6