laravelのserverlessといえばBrefらしいぞっと
やってみよう。まあvaporという手もあるだろうけど
大前提
とりあえずadministrator級権限のIAMを持つAWS Key と secretを準備してのawscliが使える事。権限を絞る事もできる気もするけどよくわからん。管理は慎重に。
install
serverless frameworkを使う
npm install -g serverless
こういうのはもうrootでインストールしちゃってもいいんじゃねえかなという気も…
serverless config credentials --provider aws --key <key> --secret <secret>
デモプロジェクトを1つ作る
いつものように、laravel.buildが早い、か、も
% curl -s "https://laravel.build/bref?with=mysql" | bash
まあ、相変わらず壊れてますが
ERROR: Invalid interpolation format for "laravel.test" option in service "services": "${APP_PORT:-80}:80"
ERROR: Invalid interpolation format for "laravel.test" option in service "services": "${APP_PORT:-80}:80"
ERROR: Invalid interpolation format for "laravel.test" option in service "services": "${APP_PORT:-80}:80"
ERROR: Invalid interpolation format for "laravel.test" option in service "services": "${APP_PORT:-80}:80"
version: '3' をdocker-compose.yamlに足しといてください
composer
composer require bref/bref
まあ、sailの場合だと、とにかくsail upしてからの
% ./vendor/bin/sail composer require bref/bref
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been updated
Running composer update bref/bref
Loading composer repositories with package information
Updating dependencies
Lock file operations: 6 installs, 0 updates, 0 removals
- Locking bref/bref (2.1.3)
- Locking crwlr/query-string (v1.0.3)
- Locking hollodotme/fast-cgi-client (v3.1.7)
- Locking nyholm/psr7 (1.8.0)
- Locking psr/http-server-handler (1.0.2)
- Locking riverline/multipart-parser (2.1.1)
bref init
そうすると
% ./vendor/bin/sail php ./vendor/bin/bref init
The `serverless` command is not installed.
You will not be able to deploy your application unless it is installed
Please follow the instructions at https://bref.sh/docs/installation.html
If you have the `serverless` command available elsewhere (eg in a Docker container) you can ignore this warning.
What kind of application do you want to create? (you will be able to add more functions later by editing `serverless.yml`)
[0] Web application (default)
[1] Event-driven function
>
こんな感じで起動してくる。ここでは0だ
What kind of application do you want to create? (you will be able to add more functions later by editing `serverless.yml`)
[0] Web application (default)
[1] Event-driven function
> 0
Creating index.php
index.php successfully created and added to git automatically.
Creating serverless.yml
serverless.yml successfully created and added to git automatically.
Project initialized and ready to test or deploy.
そうすると、こんな具合のyamlが配置される
serverless.yaml
service: app
provider:
name: aws
region: us-east-1
plugins:
- ./vendor/bref/bref
functions:
api:
handler: index.php
description: ''
runtime: php-82-fpm
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
events:
- httpApi: '*'
# Exclude files from deployment
package:
patterns:
- '!node_modules/**'
- '!tests/**'
とりあえずap-northeast-1にでもしておこう。
serverless-deploy
% serverless deploy
Deploying app to stage dev (ap-northeast-1)
⠧ Creating CloudFormation stack (9s)
endpoint: ANY - https://****.execute-api.ap-northeast-1.amazonaws.com
そうすると、指定されたurlにアクセスしてみれば、Hello there,と表示される。これは、生成されたindex.phpによるものである。
laravelの表示が見たい
そう、ここでは何かよくわからんindex.phpなど必要ない
bref/laravel-bridgeのinstall
% ./vendor/bin/sail composer require bref/laravel-bridge
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been updated
Running composer update bref/laravel-bridge
Loading composer repositories with package information
Updating dependencies
Lock file operations: 4 installs, 0 updates, 0 removals
- Locking aws/aws-crt-php (v1.2.2)
- Locking aws/aws-sdk-php (3.281.4)
- Locking bref/laravel-bridge (1.2.3)
- Locking mtdowling/jmespath.php (2.7.0)
ここでrootに展開されているserverless.yamlとindex.phpには消滅してもらう
% rm index.php serverless.yml
で
% ./vendor/bin/sail artisan vendor:publish --tag=serverless-config
INFO Publishing [serverless-config] assets.
Copying file [vendor/bref/laravel-bridge/config/serverless.yml] to [serverless.yml] ...................... DONE
server.xmlは大分変わってますな
service: laravel
provider:
name: aws
# The AWS region in which to deploy (us-east-1 is the default)
region: us-east-1
# The stage of the application, e.g. dev, production, staging… ('dev' is the default)
stage: dev
runtime: provided.al2
package:
# Directories to exclude from deployment
patterns:
- '!node_modules/**'
- '!public/storage'
- '!resources/assets/**'
- '!storage/**'
- '!tests/**'
functions:
# This function runs the Laravel website/API
web:
handler: public/index.php
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
layers:
- ${bref:layer.php-80-fpm}
events:
- httpApi: '*'
# This function lets us run artisan commands in Lambda
artisan:
handler: artisan
timeout: 120 # in seconds
layers:
- ${bref:layer.php-80} # PHP
- ${bref:layer.console} # The "console" layer
plugins:
# We need to include the Bref plugin
- ./vendor/bref/bref
結局regionを書き換えて、そしてphpのバージョンも場合によっては書き換える。こちらはlaravel10ということもあり82を指定した
% serverless deploy
Deploying laravel to stage dev (ap-northeast-1)
⠼ Packaging (2s)
提供されるurlでwelcomページが表示される
breezeを使ってみる
% ./vendor/bin/sail composer require laravel/breeze --dev
% ./vendor/bin/sail artisan breeze:install blade
この段階でdeployすると、、、assetsがnot foundとなる。これはもういたしかたなく、s3を経由しないといけないっぽい、それに関してはまた次回以降設定していこう。
この記事が気に入ったらサポートをしてみませんか?