0
0

#07 AWS(APIGateway/Lambda) + Serverless Framework + BrefでサーバレスなLaravel環境を構築してみる②

Posted at

はじめに

今回は前回の記事「AWS(APIGateway/Lambda) + Serverless Framework + BrefでサーバレスなLaravel環境を構築してみる①」の続きになります。前回はPHP動作環境を構築してみましたので、今回は記事のタイトルにある通りLaravel動作環境をAWSにデプロイしてみます。

1. 前提条件

AWSの各種サービスに対する基本的な知識、AWS CLI設定済み、npm/composerインストール済み、Serverless Frameworkインストール済み

2. 動作環境

npm:9.6.1
composer:2.1.14
AWS CLI:2.5.5
Serverless Framework:3.2.81
Bref:1.7
PHP : 8.2.1
Laravel : 10.4.1

3. 今回利用するツールに関して

前回利用したBrefとServerless Frameworkに加えて、AWSでLaravelを実行するためにLaravel-Brefを利用します。

Laravel-Brefとは

Laravel-BrefパッケージはLaravelをAWS Lambda上で実行するために必要な各種設定を自動的に行ってくれるパッケージです。
ログの出力先をAWS上のCloudWatchに設定したり、セッション保存先としてRDSやDynamoDBにするための設定なども行うことができます。
より詳しい設定を行いたい場合は、こちらLINKを確認することをお勧めします。

4. Laravel動作環境をAWSにデプロイしてみる

Brefの公式ドキュメントに沿って、Laravel動作環境をAWSにデプロイしてみます。
Bref公式ドキュメント:https://bref.sh/docs/

① Laravelプロジェクトを作成する

composer create-project laravel/laravel sample-app

② BrefとLaravel-Brefをインストールする

cd sample-app *** 作成したLaravelプロジェクトのディレクトリに移動
composer require bref/bref bref/laravel-bridge --update-with-dependencies

③ serverless.yamlを生成する

php artisan vendor:publish --tag=serverless-config            

INFO Publishing [serverless-config] assets.  

Copying file [vendor/bref/laravel-bridge/stubs/serverless.yml] to [serverless.yml] .......................................................... DONE

serverless.ymlがルートディレクトリ直下に生成されます。
AWSのデプロイ先のリージョンの設定がこのファイルで行うことが可能です。
デフォルトのデプロイ先が、バージニア(us-east-1)になっているので、
ap-northeast-1(東京)リージョンに変更しておくと良いです。

serverless.yml
provider:
  name: aws
  # The AWS region in which to deploy (us-east-1 is the default)
  region: ap-northeast-1 *** デフォルトだとバージニア(us-east-1)がデプロイ先のリージョンになっている
・・・省略

④ Laravelのキャッシュを削除する

php artisan config:clear                         

INFO Configuration cache cleared successfully.

キャッシュに関しては以下のように記載されています。php artisan config:cacheを実行する必要はないようですが、php artisan config:clearは実行しておきましょう。

We do not want to deploy "dev" caches that were generated on our machine (because paths will be different on AWS Lambda). Let's clear them before deploying: php artisan config:clear
When running in AWS Lambda, the Laravel application will automatically cache its configuration when booting. You don't need to run php artisan config:cache before deploying.
出典:https://bref.sh/docs/frameworks/laravel.html#deployment

⑤ Laravelをデプロイする

デプロイを実行するとエンドポイントが表示されるので、アクセスするとサンプルページが表示されます。

serverless deploy                

Deploying laravel to stage dev (us-east-1)

✔ Service deployed to stack laravel-dev (127s)

endpoint: ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com **** 前回と同じようにLaravelのエンドポイントが表示されます

functions:
 web: laravel-dev-web (31 MB)
 artisan: laravel-dev-artisan (31 MB)

Need a better logging experience than CloudWatch? Try our Dev Mode in console: run "serverless --console"

最後に

前回とは多少の手順の違いはあれど、これだけでAWSのLambda上にLaravel動作環境を構築できてしまいます。
Laravelのソースコードに追加機能を実装したり、バクを修正した場合にも同様に簡単にデプロイすることができます。
サーバーレスアーキテクチャ自体ここ何年も注目されているものではありますが、
PHP/LaravelをLambdaで実行するための技術として、BrefやServelessFrameworkは今後より注目されていく技術なのではないかと思います。
この記事を読んで興味を持った方はぜひ、ご自身の手でAWS/Bref/ServelessFrameworkを利用してみていただけたらと思います。

0
0
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
0
0