Serverless architecture has grown more popular since Amazon Web Services (AWS) introduced Lambda. Serverless does not mean computing without servers as the name suggests, but it means delegating the responsibility of taking care of the infrastructure (servers, operating systems, disk space, software updates) to the cloud provider (AWS, Azure, Google Cloud). Serverless allows the developer to focus only on the code itself.
Serverless code is often created in the form of functions, and that’s why this architecture is also called “Function as a Service (FaaS)”. This computation model places the responsibility of managing the servers on the cloud provider and frees the developer’s time to work on the product.
NEW RESEARCH: LEARN HOW DECISION-MAKERS ARE PRIORITIZING DIGITAL INITIATIVES IN 2024.
Security continues to be the developer’s responsibility. There are a collection of guidelines and tools on Serverless security and Modus Create provides application security consulting, designed to enumerate threats, vulnerabilities, and risks. The results of this engagement are presented from a tactical and strategic perspective. With these findings, you can then take the next step in improving your application’s security posture.
The New LAMP Stack: Serverless on AWS
To face the challenges of scaling applications, AWS is offering new services to help PHP developers build reliable and efficient serverless applications. In comparison to traditional LAMP (Linux, Apache, MySQL, PHP) stack, serverless LAMP (Lambda, API Gateway, MySQL, PHP) applications are easier to scale and maintain while costing less than the alternative (using a pay-per-use model).
The Laravel framework has a healthy list of tools that enable easy deployment of a serverless application (including Laravel Vapor, Vercel). In this tutorial, I’ll be covering how to use Bref to build a serverless Laravel application.
Step 1: AWS User
First, we need to create an account for our application in AWS. To do this, you should go to the IAM management console of your account and create a new user with Programmatic access.
After following the user creation process, it’s important to securely store the access key ID and secret access key. Those will be used for your application to use AWS resources.
Step 2: Serverless Framework
After configuring your AWS account, it’s time to install and configure your serverless framework. Here we will use the access key ID and secret access key generated in the previous step.
npm install -g serverless serverless config credentials --provider aws --key --secret
Step 3: Laravel + Bref
Now it’s time to create your Laravel application, installing the most recent version, and creating a new app.
composer global require laravel/installer laravel new serverless-app
To have Bref configured with Laravel we also need to install the laravel-bridge component.
composer require bref/bref bref/laravel-bridge
Then let’s create a configuration file for serverless (serverless.yml).
php artisan vendor:publish --tag=serverless-config 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 exclude: - 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-74-fpm} events: - httpApi: '*' # This function lets us run artisan commands in Lambda artisan: handler: artisan timeout: 120 # in seconds layers: - ${bref:layer.php-74} # PHP - ${bref:layer.console} # The "console" layer plugins: # We need to include the Bref plugin - ./vendor/bref/bre
Step 4: Deploy
Before deploying, we need to clear any configuration change generated on our machine. After that, we’re ready to deploy.
php artisan config:clear serverless deploy
This post was published under the Laravel Community of Experts. Communities of Experts are specialized groups at Modus that consolidate knowledge, document standards, reduce delivery times for clients, and open up growth opportunities for team members. Learn more about the Modus Community of Experts program in the article Building a Community of Experts.
References
Guido Percú
Related Posts
-
Build Secure Web Applications with Laravel
For modern web applications, reliability and security are top priority. The Modus Laravel Community of…
-
Serverless - AllTheThings (Part 1 of 3)
Serverless software architecture reduces maintenence and operation costs of computing, provides scalable, on demand data…