Using MassTransit to connect to existing AWS SNS/SQS

Viewed 591

We have SNS/SQS already setup and working, but we'd like to start moving away from AWSSDK.* and towards MassTransit.

I have successfully played around with MassTransit using both the InMemory and RabbitMq (via docker). However, I am having trouble getting MassTransit working with AWS. Everything I try results in startup errors, such as "Endpoint faulted"

All of the examples I can find seem to allow MassTransit to instantiate any queues/topics necessary according to its own naming conventions or do not show a complete configuration. I cannot find an example that explains what values to use.

Suppose I have grabbed the following strings from the AWS management console.

SNS
Name: "SNS"
DisplayName: "SNS.DisplayName"
ARN: "SNS.ARN" <- this is what I give to AWSSDK.SimpleNotificationService

SNS -> Subscription
ARN: "SNS.Sub.ARN"
Endpoint: "SNS.Sub.Endpoint
Topic: "SNS.Sub.Topic"

SQS
Name: "SQS"
ARN: "SQS.ARN"
URL: "SQS.Url"

Suppose I have this configuration:

.AddMassTransit(x =>
{
    x.AddConsumer<MyConsumer>();

    x.UsingAmazonSqs((context, cfg) =>
    {
        cfg.Host("us-west-2", h =>
        {
            h.AccessKey("access");
            h.SecretKey("secret");
        });

        cfg.ReceiveEndpoint(">> What goes here? <<", e =>
        {
            e.UseMessageRetry(r => r
                .Interval(2, TimeSpan.FromMilliseconds(5000))
            );

            e.ConfigureConsumeTopology = false;
            e.Subscribe(">> What goes here? <<", s =>
            {
                s.TopicAttributes["DisplayName"] = ">> What goes here? <<";
                // Is this needed?
                // s.TopicSubscriptionAttributes["some-subscription-attribute"] = "some-attribute-value";
            });

            e.ConfigureConsumer<MyConsumer>(context);
        });
    });
})
.AddMassTransitHostedService()

Which values need to go where in order to be able to send/publish and consume messages? Am I missing any configuration steps?

Thanks.

1 Answers

MassTransit will create SNS topics, SQS queues, and configure SNS->SQS subscriptions for you. There is no need to configure them in the management console, just make sure the IAM credentials are able to create those resources. If they already exist, they will not be re-created.

.AddMassTransit(x =>
{
    x.AddConsumer<MyConsumer>();

    x.UsingAmazonSqs((context, cfg) =>
    {
        cfg.Host("us-west-2", h =>
        {
            h.AccessKey("access");
            h.SecretKey("secret");
        });

        cfg.ReceiveEndpoint("SQS queue name", e =>
        {
            e.UseMessageRetry(r => r.Interval(2, TimeSpan.FromMilliseconds(5000)));

            e.ConfigureConsumeTopology = false;
            e.Subscribe("SNS topic name");

            e.ConfigureConsumer<MyConsumer>(context);
        });
    });
})
.AddMassTransitHostedService()
Related