How to use Amazon SQS with Symfony 4

Recently to implement the e-mail sending during the order creation, PHP programmers were using CRON. It's easy and simple, because e-mails are sending by batches using CRON schedule. But what if we need to send a letter once? Queues can be an aid, although a lot of features can be implemented by using queues.

I need to create an internal service that would be built on Symfony 4 while I was working on it, customer have asked me to use a queue for our task, so I’ve decided to test Symfony – Messenger (it’s in experimental stage now). And there was a problem, since the pipe “off the box” was only for RabbitMQ, but our requirements were based on the Amazon SQS. By writing this article I want to make life easier or PHP developers, who are facing similar challenges.

Amazon SQS

By using “service filter” we must find "SQS" (Simple Queue Service) and click to create a new queue. After queue created we can click on it and find it’s details – URL in the following format - https://sqs.YOUR USER ID/QUEUE NAME.

In order to send messages to this queue we must create a user, so by using “service filter” we are searching for "IAM" (Identity and Access Management) and creating new user with the Access Type - "Programmatic access". In the “Permissions” tab we should create new “SQS” group and with the “sqs” filer searching “AmazonSQSFullAccess” and choosing this selected group for our user. Then we should copy “Access Key ID” and “Secret Access Key” and move back to our Symfony project.


  • composer require symfony/messenger
  • composer require enqueue/messenger-adapter (an adapter which allows the usage of project transports Enqueue)
  • composer require enqueue/sqs (transport for Amazon SQS)

Symfony 4

  • Create a message class, which will be sent, see: src/Piatrouski/ExampleSqs/Message/Example.php
  • Create a handler, which will handle received messages, see: src/Piatrouski/ExampleSqs/Handler/Example.php
  • Put our handler in the config/services.yaml property and add "tags: [messenger.message_handler]"

For example, I have created a helper to demonstrate the processing of messages in the handler (src/Piatrouski/ExampleSqs/Helper/Data.php) and the console command to send test messages from CLI (src/Piatrouski/ExampleSqs/Command/SqsTest.php)

The next step is parameters setup, so go to "config/packages/messenger.yaml" and replace the topic[name] and queue[name] values for our queue name, and in "routing" section put message class and transport which will be used, in our case - "sqs".

The rest is to create or make changes in .env.local file where we should put amazon user credentials like "ENQUEUE_DSN=sqs:?key=YOUR ACCESS KEY ID&secret=YOUR SECRET ACCESS KEY&region=YOUR SQS REGION"

That’s all! Now we a executing our "bin/console example-sqs:test" from the console to send a message, and then run “bin/console messenger:consume-messages sqs -vvv" and tracking for message receiving.