Download the PHP package bref/symfony-messenger without Composer
On this page you can find all versions of the php package bref/symfony-messenger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package symfony-messenger
Bridge to use Symfony Messenger on AWS Lambda with Bref.
This bridge allows messages to be dispatched to SQS, SNS or EventBridge, while workers handle those messages on AWS Lambda.
Installation
This guide assumes that:
- Symfony is installed
- Symfony Messenger is installed
- Bref is installed and configured to deploy Symfony
First, install this package:
Next, register the bundle in config/bundles.php
:
SQS, SNS and EventBridge can now be used with Symfony Messenger.
Usage
Symfony Messenger dispatches messages. To create a message, follow the Symfony Messenger documentation.
To configure where messages are dispatched, all the examples in this documentation are based on the example from the Symfony documentation:
SQS
The SQS service is a queue that works similar to RabbitMQ. To use it, set its URL in the environment variable MESSENGER_TRANSPORT_DSN
:
That's it, messages will be dispatched to that queue.
The implementation uses the SQS transport provided by Symfony Amazon SQS Messenger, so all those features are supported. If you already use that transport, the transition to AWS Lamdba is very easy and should not require any change for dispatching messages.
Create the SQS queue
You can create the Queue yourself in the Console, write custom Cloudformation or use Lift's Queue construct that will handle that for you.
Here is a simple example with Lift, make sure to install the plugin first and check out the full documentation for more details.
In all cases, you would want to disable auto_setup
to avoid extra requests and permission issues.
Add permissions
When running Symfony on AWS Lambda, it is not necessary to configure credentials. The AWS client will read them from environment variables automatically.
You just have to provide the correct role statements in serverless.yml
and Lambda will take care of the rest. The required IAM permission to publish to SQS using Messenger is sqs:SendMessage
on the given queue.
If you use Lift, this is done automatically for you.
Consume messages from SQS
-
If you don't use Lift, create the function that will be invoked by SQS in
serverless.yml
: - Create the handler script (for example
bin/consumer.php
):
If you are using Symfony 5.1 or later, use this instead:
- Register and configure the
SqsConsumer
service:
Now, anytime a message is dispatched to SQS, the Lambda function will be called. The Bref consumer class will put back the message into Symfony Messenger to be processed.
FIFO Queue
The FIFO queue guarantees
exactly once delivery, and has a mandatory queue name suffix .fifo
:
Symfony Amazon SQS Messenger will automatically calculate/set
the MessageGroupId
and MessageDeduplicationId
parameters required for FIFO queues, but you can set them explicitly:
Everything else is identical to the normal SQS queue.
SNS
AWS SNS is "notification" instead of "queues". Messages may not arrive in the same order as sent, and they might arrive all at once. To use it, create a SNS topic and set it as the DSN:
That's it, messages will be dispatched to that topic.
Note: when running Symfony on AWS Lambda, it is not necessary to configure credentials. The AWS client will read them from environment variables automatically.
To consume messages from SNS:
-
Create the function that will be invoked by SNS in
serverless.yml
: - Create the handler script (for example
bin/consumer.php
):
If you are using Symfony 5.1 or later, use this instead:
- Register and configure the
SnsConsumer
service:
Now, anytime a message is dispatched to SNS, the Lambda function will be called. The Bref consumer class will put back the message into Symfony Messenger to be processed.
EventBridge
AWS EventBridge is a message routing service. It is similar to SNS, but more powerful. To use it, configure the DSN like so:
Optionally you can add set the EventBusName via a event_bus_name
query parameter, either the name or the ARN:
That's it, messages will be dispatched to EventBridge.
Note: when running Symfony on AWS Lambda, it is not necessary to configure credentials. The AWS client will read them from environment variables automatically.
To consume messages from EventBridge:
-
Create the function that will be invoked by EventBridge in
serverless.yml
: - Create the handler script (for example
bin/consumer.php
):
If you are using Symfony 5.1 or later, use this instead:
- Register and configure the
EventBridgeConsumer
service:
Now, anytime a message is dispatched to EventBridge for that source, the Lambda function will be called. The Bref consumer class will put back the message into Symfony Messenger to be processed.
Error handling
AWS Lambda has error handling mechanisms (retrying and handling failed messages). Because of that, this package does not integrates Symfony Messenger's retry mechanism. Instead, it works with Lambda's retry mechanism.
This section is work in progress, feel free to contribute to improve it.
When a message fails with SQS, by default it will go back to the SQS queue. It will be retried until the message expires. Here is an example to setup retries and "dead letter queue" with SQS:
When using SNS and EventBridge, messages will be retried by default 2 times.
Configuration
Configuring AWS clients
By default, AWS clients (SQS, SNS, EventBridge) are preconfigured to work on AWS Lambda (thanks to environment variables populated by AWS Lambda).
However, it is possible customize the AWS clients, for example to use them outside of AWS Lambda (locally, on EC2…) or to mock them in tests. These clients are registered as Symfony services under the keys:
bref.messenger.sqs_client
bref.messenger.sns_client
bref.messenger.eventbridge_client
For example to customize the SQS client:
Automatic transport recognition
Automatic transport recognition is primarily handled by default through TransportNameResolvers for SNS and SQS,
ensuring that the transport name is automatically passed to your message handlers.
However, in scenarios where you need to manually specify the transport name or adjust the default behavior,
you can do so by setting the $transportName
parameter in your service definitions within the config/services.yaml file.
This parameter should match the transport name defined in your config/packages/messenger.yaml.
For instance, for a SNSConsumer, you would configure it as follows:
Disabling transports
By default, this package registers Symfony Messenger transports for SQS, SNS and EventBridge.
If you want to disable some transports (for example in case of conflict), you can remove BrefMessengerBundle
from config/bundles.php
and reconfigure the transports you want in your application's config. Take a look at Resources/config/services.yaml
to copy the part that you want.
Customizing the serializer
If you want to change how messages are serialized, for example to use Happyr message serializer, you need to add the serializer on both the transport and the consumer. For example:
All versions of symfony-messenger with dependencies
ext-json Version *
async-aws/sns Version ^1.0
async-aws/sqs Version ^1.2|^2.0
async-aws/event-bridge Version ^1.0
bref/bref Version ^1.5 || ^2.0
symfony/amazon-sqs-messenger Version ^5.4 || ^6.0 || ^7.0
symfony/config Version ^5.4 || ^6.0 || ^7.0
symfony/dependency-injection Version ^5.4 || ^6.0 || ^7.0
symfony/http-kernel Version ^5.4 || ^6.0 || ^7.0
symfony/messenger Version ^5.4 || ^6.0 || ^7.0
symfony/yaml Version ^5.4 || ^6.0 || ^7.0