Installation:
composer require enqueue/sqs
Ensure your Laravel project has the enqueue/amqp-laravel or enqueue/doctrine package for integration (SQS is a transport, not a full queue system).
Configuration:
Add SQS transport to your Laravel queue configuration (config/queue.php):
'connections' => [
'sqs' => [
'driver' => 'sqs',
'host' => env('AWS_SQS_HOST', 'sqs.us-east-1.amazonaws.com'),
'region' => env('AWS_REGION', 'us-east-1'),
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
],
'queue' => env('SQS_QUEUE_URL', 'your-queue-url'),
'options' => [
'prefix' => env('SQS_PREFIX', ''),
'suffix' => env('SQS_SUFFIX', ''),
],
],
],
First Use Case: Dispatch a job using SQS:
use Illuminate\Support\Facades\Queue;
Queue::connection('sqs')->push(new YourJobClass);
config/queue.php – Laravel queue configuration.app/Console/Kernel.php – Queue worker setup.Job Dispatching: Use SQS for async tasks (e.g., sending emails, processing uploads):
Queue::connection('sqs')->push(
new SendWelcomeEmail($user),
'emails' // Optional custom queue name
);
Worker Setup: Run the SQS worker via Artisan:
php artisan queue:work sqs --queue=emails --sleep=3 --tries=3
--sleep: Delay between checks (seconds).--tries: Max retries for failed jobs.Batch Processing:
Use batch() for grouped jobs:
Queue::connection('sqs')->batch([])->push(new ProcessOrdersJob);
Delayed Jobs:
Schedule jobs with delay():
Queue::connection('sqs')->later(now()->addMinutes(10), new NotifyUserJob);
Laravel Events: Bind SQS to event dispatching:
event(new UserRegistered($user))->onQueue('sqs', 'users');
Retry Logic:
Leverage SQS visibility timeout (default: 30s) for retries. Adjust in config/queue.php:
'options' => [
'visibility_timeout' => 60, // Seconds
],
Monitoring: Use AWS CloudWatch or SQS metrics to track queue depth/latency.
Credentials Management:
.env or AWS IAM roles (for EC2/ECS).aws configure.Visibility Timeouts:
Queue::later() for long tasks.Queue Naming:
snake_case).'options' => ['prefix' => 'laravel_app_'],
Message Size Limits:
Worker Stuck on Messages:
aws sqs change-message-visibility --queue-url YOUR_QUEUE_URL --receipt-handle RECEIPT_HANDLE --visibility-timeout 0
Enable Logging:
Add to config/queue.php:
'log' => env('QUEUE_LOG', true),
'log_file' => storage_path('logs/queue.log'),
Check Dead Letter Queues (DLQ):
Configure DLQ in config/queue.php:
'options' => [
'dead_letter_queue' => 'https://sqs.us-east-1.amazonaws.com/1234567890/dlq',
],
Failed jobs auto-route to DLQ after max retries.
Custom Middleware: Attach middleware to SQS jobs (e.g., logging, rate limiting):
Queue::connection('sqs')->push(new YourJob)
->onQueue('high_priority')
->delay(10)
->middleware([\App\Middleware\LogJob::class]);
Extend Transport:
Override SQS behavior by creating a custom transport class (extend Enqueue\Sqs\SqsContext).
Use Raw SQS API:
For advanced use cases, inject the Aws\Sqs\SqsClient into a service:
use Aws\Sqs\SqsClient;
use Enqueue\Sqs\SqsContext;
$sqs = new SqsClient([...]);
$context = new SqsContext($sqs, $config);
Cross-Account Access: Use IAM roles or cross-account policies if accessing SQS across AWS accounts. Example policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["sqs:*"],
"Resource": "arn:aws:sqs:us-east-1:1234567890:your-queue"
}]
}
How can I help you explore Laravel packages today?