Recently, I had to migrate my queue driver on a project from Redis to SQS and for some weird reason the information provided on the official Laravel Queues documentation didn't do justice to this and I couldn't find an article online that could help, that's why I'm writing this, with the hope that you don't spend two hours of your Saturday morning figuring stuff that should have been documented. I'd try to keep this article as brief as possible, I'd attach links to other articles that explain some steps so I can focus on the important parts.
- An existing Laravel application
- An AWS account
- An understanding of Laravel queues
- Some AWS knowledge
Run the following command to install the SDK:
composer require aws/aws-sdk-php
Click on this link to watch the guide.
When you get to the point where you need to set permissions, search for
AmazonSQSFullAccess and select it.
If you already have existing credentials, modify its policies and do the same.
Head over to your SQS console, click on
Out of the box, Laravel dispatches jobs to the
default queue, therefore, your first queue should be named
default on SQS.
If you would be dispatching to a queue other than
default, the queue name should be the same as the queue you're dispatching to.
Leave everything else at its default, scroll to the bottom and click on the
create queue button.
.env with your preferred text editor. Add your AWS keys in the following format:
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY AWS_DEFAULT_REGION=YOUR_SQS_REGION
Ensure you replace
YOUR_* with the right credentials. Next, set your SQS Prefix:
| I don't think I need to explain what
You can get
<your-account-id> on your AWS console, click on your account name on the top right of your dashboard.
you'd find your account ID just beside
My Account as shown above. Finally, change the queue driver to SES.
Run the following command to start your queue worker:
php artisan queue:work sqs
Dispatch your job on the default queue and it should be processed.
If you use Laravel Horizon to manage your queues, you'd be making some modifications to your
horizon.php file. For each worker defined, change the queue connection from
redis which is the default to
<?php ... 'defaults' => [ 'queue_name' => [ ... 'connection' => 'redis', ], ... ] ...
<?php ... 'defaults' => [ 'queue_name' => [ ... 'connection' => env('QUEUE_CONNECTION'), ], ... ] ...
After these modifications have been made, restart horizon and all should be good.
I have nothing else to add, have fun, keep building.