Are you wondering how to set up Serverless Laravel using AWS Lambda? Our AWS Support team is here to lend a hand with your queries and issues.
How to setup Serverless Laravel using AWS Lambda
Did you know that Laravel Vapor runs on serverless technology? In other words, we don’t have physical servers. Hence, it enables our application to automatically scale on-demand, and we pay for only what we need.
On the other hand, AWS Lambda allows us to run code as a function as a response to other AWS services events.
Additionally, it allows us to listen to API gateway events, thereby enabling us to build a web service that is completely event driven by running a function call per HTTP request.
Today, our experts are going to take us through how to set up Serverless Laravel using AWS Lambda. It involves the following steps:
- Install bref
- Deploy to AWS Lambda using Serverless
- Configure Laravel queue to use SQS
How to install bref
According to our experts, we can easily install bref onto an existing Laravel application
$ composer require bref/bref bref/laravel-bridge
Then, we have to create a serverless.yml file, with the following command:
$ php artisan vendor:publish –tag=serverless-config
This brings us to the default configuration. It has to be modified slightly to handle Laravel queue jobs.
The configuration file will mention a PHP file that is responsible for SQS events in AWS Lambda.
For instance:
$kernel->bootstrap();return $app->makeWith(LaravelSqsHandler::class, [
'connection' => 'sqs', // this is the Laravel Queue connection
'queue' => getenv('SQS_QUEUE'),
]);
How to Deploy to AWS Lambda using Serverless
- First, install serverless via one of the following commands:
$ npm install -g serverless
Or
$ yarn global add serverless
- Next, we have to create AWS access keys and configure it with this command:
$ serverless config credentials --provider aws --key <key> --secret <secret>
- Finally, we will deploy the Laravel application on our AWS account:
$ serverless deploy
How to configure Laravel queue to use SQS
If the above deployment was successful, it will result in 2 SQL queues on our AWS account. We need to locate the main queue’s name and put it on .env as SQS_QUEUE as seen here:
# .env
QUEUE_CONNECTION=sqs
SQS_PREFIX=https://sqs.us-east-1.amazonaws.com/<your-account-id>
SQS_QUEUE=<generated-queue-name-from-aws>
AWS_ACCESS_KEY_ID=<key>
AWS_SECRET_ACCESS_KEY=<secret>
AWS_DEFAULT_REGION=us-east-1
Then, we have to add a token config to the config/queue.php file as seen here :
'sqs' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'token' => env('AWS_SESSION_TOKEN'),
...
At this point, we can dispatch the job as a general Laravel job.
Let us know in the comments if you need any additional help with the above steps.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
To sum up, our Support Engineers demonstrated how to set up Serverless Laravel using AWS Lambda with ease.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments