Skip to content

Instantly share code, notes, and snippets.

@thesafaraliyev
Last active December 13, 2024 14:32
Show Gist options
  • Save thesafaraliyev/1621ab75cc92b860efd0cb2e7a2fd5cb to your computer and use it in GitHub Desktop.
Save thesafaraliyev/1621ab75cc92b860efd0cb2e7a2fd5cb to your computer and use it in GitHub Desktop.
Laravel with Localstack S3 and SQS setup
FILESYSTEM_DRIVER=s3
QUEUE_CONNECTION=sqs
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=bucket1
AWS_ENDPOINT=http://localhost:4566/
AWS_USE_PATH_STYLE_ENDPOINT=true
SQS_QUEUE=queue1
SQS_PREFIX=http://localhost:4566/000000000000/

Amazon CLI commands to manage S3 and SQS in Localstack and Laravel setup guideline.

S3 commands

To create new bucket:

aws --endpoint-url=http://localhost:4566 s3 mb s3://your-bucket-name

Object list in bucket:

aws s3api list-objects-v2 --bucket your-bucket-name --endpoint-url http://localhost:4566

SQS commands

To create new queue

aws --endpoint-url=http://localhost:4566 sqs create-queue --queue-name your-queue-name

List available queues:

aws --endpoint-url=http://localhost:4566 sqs list-queues

Job list in queue:

  • The latest job:
    aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url http://localhost:4566/000000000000/your-queue-name
  • The latest 10 jobs:
    aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url http://localhost:4566/000000000000/your-queue-name --max-number-of-messages 10

Purge queue jobs:

aws --endpoint-url=http://localhost:4566 sqs purge-queue --queue-url=http://localhost:4566/000000000000/your-queue-name

Delete specific queue:

aws --endpoint-url=http://localhost:4566 sqs delete-queue --queue-url http://localhost:4566/000000000000/your-queue-name

Laravel configuration

S3 setup

  1. Set FILESYSTEM_DRIVER key value to s3.
  2. Set AWS_USE_PATH_STYLE_ENDPOINT to true
  3. Create a new bucket and set AWS_BUCKET.
  4. Set AWS_ENDPOINT to your host (default localhost).
  5. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY can be omitted since Localstack doesn't check them.

SQS setup

  1. Change QUEUE_CONNECTION key value to sqs.
  2. Create new a queue and set its name to SQS_QUEUE.
  3. Set SQS_PREFIX to QueueUrl which returned after creating new queue without queue name suffix.
@gstv57
Copy link

gstv57 commented Jul 2, 2024

thanks !

@peterfox
Copy link

An additional note: if you run the php artisan queue:monitor command with localstack SQS, it'll fail. To fix this you need to add:

 'endpoint' => env('AWS_ENDPOINT', ''),

to your sqs driver in queues.php config e.g.

'sqs' => [
    'driver' => 'sqs',
    'prefix' => env('SQS_PREFIX', ''),
    'queue' => 'default',
    'region' => 'us-west-1',
    'endpoint' => env('AWS_ENDPOINT', ''),
],

Then set AWS_ENDPOINT to http://localhost:4566 in your .env otherwise you'll get an error like the following:

Error executing "GetQueueAttributes" on "https://sqs.us-west-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://sqs.us-west-1.amazonaws.com` resulted in a `403 Forbidden` response:
{"__type":"com.amazon.coral.service#UnrecognizedClientException","message":"The security token included in the request i (truncated...)
 InvalidClientTokenId (Sender): The security token included in the request is invalid. - {"__type":"com.amazon.coral.service#UnrecognizedClientException","message":"The security token included in the request is invalid."}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment