# Amazon SQS

MassTransit.AmazonSQS

MassTransit combines Amazon SQS (Simple Queue Service) with SNS (Simple Notification Service) to provide both send and publish support.

Configuring a receive endpoint will use the message topology to create and subscribe SNS topics to SQS queues so that published messages will be delivered to the receive endpoint queue.

In the example below, the Amazon SQS settings are configured.

namespace AmazonSqsConsoleListener
{
    using System;
    using System.Threading.Tasks;
    using MassTransit;
    using Microsoft.Extensions.DependencyInjection;

    public class Program
    {
        public static async Task Main()
        {
            var services = new ServiceCollection();
            services.AddMassTransit(x =>
            {
                x.UsingAmazonSqs((context, cfg) =>
                {
                    cfg.Host("us-east-2", h =>
                    {
                        h.AccessKey("your-iam-access-key");
                        h.SecretKey("your-iam-secret-key");

                        // following are OPTIONAL

                        // specify a scope for all queues
                        h.Scope("dev");

                        // scope topics as well
                        h.EnableScopedTopics();
                    });
                });
            });
        }
    }
}

The configuration includes:

  • The Amazon SQS host
    • Region name: us-east-2
    • Access key and secret key used to access the resources

Any topic can be subscribed to a receive endpoint, as shown below. The topic attributes can also be configured, in case the topic needs to be created.

namespace AmazonSqsReceiveEndpoint
{
    using System;
    using System.Threading.Tasks;
    using MassTransit;
    using Microsoft.Extensions.DependencyInjection;

    public class Program
    {
        public static async Task Main()
        {
            var services = new ServiceCollection();
            services.AddMassTransit(x =>
            {
                x.UsingAmazonSqs((context, cfg) =>
                {
                    cfg.Host("us-east-2", h =>
                    {
                        h.AccessKey("your-iam-access-key");
                        h.SecretKey("your-iam-secret-key");
                    });

                    cfg.ReceiveEndpoint("input-queue", e =>
                    {
                        // disable the default topic binding
                        e.ConfigureConsumeTopology = false;

                        e.Subscribe("event-topic", s =>
                        {
                            // set topic attributes
                            s.TopicAttributes["DisplayName"] = "Public Event Topic";
                            s.TopicSubscriptionAttributes["some-subscription-attribute"] = "some-attribute-value";
                            s.TopicTags.Add("environment", "development");
                        });
                    });
                });
            });
        }
    }
}