/serverless-laravel

This is a sample repository of Serverless LAMP stack.

Primary LanguagePHPMIT LicenseMIT

Serverless Laravel

automatic release GitHub release (latest by date) GitHub

[ English | Japanese ]

This is a sample repository of Serverless LAMP stack.

Learn more about what a serverless lamp stack is, see this AWS blog.

Introducing the new Serverless LAMP stack | AWS Compute Blog

Setup

Build the Laravel environment on docker in our local machine with Laravel Sail.

$ curl -s "https://laravel.build/serverless-laravel" | bash

# If you have a component you want, add queries like this:
# curl -s "https://laravel.build/serverless-laravel?with=mysql,redis" | bash

Build containers

$ ./vendor/bin/sail up -d

Setup serverless

Install the Bref libraries to build serverless Laravel.

Bref is a great way to easily create an environment where you can run your Laravel application on AWS Lambda.

Serverless Laravel applications - Bref

# Add libraries
$ ./vendor/bin/sail composer require bref/bref bref/laravel-bridge

# Copy yaml of serverless.
$ ./vendor/bin/sail artisan vendor:publish --tag=serverless-config

Local development

Described how to execute commands that you will often use.

# migrate
$ ./vendor/bin/sail artisan migrate

# seed
$ ./vendor/bin/sail artisan db:seed

# install npm packages
$ ./vendor/bin/sail npm i

Prepare env file.

$ cp ./serverless-config/env/stack.yml.example ./serverless-config/env/<your stack>.yml

Customize your container settings.

$ ./vendor/bin/sail artisan sail:publish
Copied Directory [/vendor/laravel/sail/runtimes] To [/docker]
Publishing complete.

Connect your local database.

# Set variables
$ MYSQL_USER=<user_name>
$ MYSQL_PASSWORD=<password>
$ MYSQL_DATABASE=<database_name>

$ docker-compose exec mysql bash -c 'mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE}'

Tips

Add aliases to your shell. ( e.g. ~/.bashrc )

# sail
alias sail="./vendor/bin/sail"

# bref
alias bref="./vendor/bin/bref"

Deployment

Dynamic application

Bia severless framework CLI to AWS Cloud. If you want to migrate or seed, use the Bref CLI.

$ seleverless deploy -v
# or
$ sls deploy -v

# migrate
$ ./vendor/bin/bref cli <artisan function> --region <region> -- migrate

# seed
$ ./vendor/bin/bref cli <artisan function> --region <region> -- db:seed

Static assets

Bia AWS CLI to S3 bucket.

$ ./vendor/bin/sail npm run prod

$ aws s3 sync public/ s3://<bucket-name>/ --delete --exclude index.php