Search code examples
c#amazon-sqsamazon-snsmasstransit

MassTransit fails to connect to existing AWS SNS/SQS


Currently, I'm attempting to move from aws sdk to MassTransit. And I have a big issue with connecting to the SQS/SNS. Our infrastructure team created sqs/sns using a terraform script. And we (as developers and application) cannot change (in any way) it since the infrastructure is managed by a separate team and another aws account/role.

We're using MassTransit 8.0 (the newest version)

Basically we have: queue:

name: bci-name-dev-aim-name

encryption: SSE-SQS

Where we have SNS subscription:

arn:aws:sqs:us-west-2::bci-name-dev-aim-name to topic: bci-name-dev-aim-name-topic (protocol SQS)

As a topic

name: bci-name-dev-aim-name-topic

encryption: default one (alias/aws/sns)

My producer configuration looks:

services.AddMassTransit(cfg =>
                {
                    cfg.UsingAmazonSqs((context, bus) =>
                    {
                        bus.UseMessageScheduler(schedulerEndpoint);
            
                        bus.Host("us-west-2", h => {}); //using env Variables

                        bus.Message<PublicationMessage>(x =>
                        {
                            x.SetEntityName("bci-name-dev-aim-name-topic");
                        });
                        
                        bus.ConfigureEndpoints(context);
                    });
                });

My consument:

services.AddMassTransit(cfg =>
                {
                    cfg.AddConsumer<PublicationMessageConsumer>();
        
                    cfg.UsingAmazonSqs((context, bus) =>
                    {                            
                        bus.Host("us-west-2", h => {});

                        
                        bus.PublishTopology.TopicAttributes.Add(QueueAttributeName.KmsMasterKeyId, "alias/aws/sns");

                        bus.ReceiveEndpoint("bci-name-dev-aim-name", ec =>  //queue name
                        {
                            // disable the default topic binding
                            ec.ConfigureConsumeTopology = false;
                            ec.QueueAttributes.Add(QueueAttributeName.KmsMasterKeyId, "alias/aws/sqs");
                            
                            ec.Subscribe("bci-name-dev-aim-name-topic");    //topic name
                            
                            ec.ConfigureConsumer<PublicationMessageConsumer>(context);
                        });
                        
                        bus.ConfigureEndpoints(context);
                    });
                });

After starting the application (console app). Producer is reports (during starting app):

  info: MassTransit[0]
  Bus started: amazonsqs://us-west-2/

But when I tried to start consumer I got:

  info: MassTransit[0]
  Configured endpoint bci-name-dev-aim-name, Consumer: Consumer.PublicationMessageConsumer

(thats's the only log so no bus started log etc)

When I stop the consumer I receive:

  warn: MassTransit[0]
  Failed to stop bus: amazonsqs://us-west-2/BKOWALCZYKW1_Consumer_bus_kotoyynbyybfbeanbdpf9sykno?durable=false&autodelete=true (Not Started)

Can you advise?

++++++ EDITED:

I tried to adapt to suggestions from @Chris Patterson. And currently my configuration looks:

Producer:

services.AddMassTransit(cfg =>
                {
                    cfg.UsingAmazonSqs((context, bus) =>
                    {
                        bus.UseMessageScheduler(schedulerEndpoint);
            
                        bus.Host("us-west-2", h => {}); //using env Variables

                        bus.Message<PublicationMessage>(x =>
                        {
                            x.SetEntityName("bci-name-dev-aim-name-topic");
                        });
                        
                        bus.ConfigureEndpoints(context);
                    });
                });

Consumer:

services.AddMassTransit(cfg =>
                {
                    cfg.AddConsumer<PublicationMessageConsumer>();
        
                    cfg.UsingAmazonSqs((context, bus) =>
                    {                            
                        bus.Host("us-west-2", h => {});

                        bus.ReceiveEndpoint("bci-name-dev-aim-name", ec =>  //queue name
                        {
                            // disable the default topic binding
                            ec.ConfigureConsumeTopology = false;
                         
                            ec.ConfigureConsumer<PublicationMessageConsumer>(context);
                        });
                        
                        bus.ConfigureEndpoints(context);
                    });
                });

And It looks like the consumer reported: "Bus started: amazonsqs://us-west-2/". So it works


Solution

  • If the SQS queue is already created, the SNS topic already created, and the subscription from SNS->SQS is already created, you should remove the .Subscribe call since it will try to create the subscription in the topic.

    Also, setting queue/topic attributes in MassTransit won't do anything since the topic and queue have already been created.

    That should configure MassTransit to work with the least number of permissions. You'll also want to likely pre-create the _error queue in the event of an error, or configure MassTransit to use the standard SQS dead-letter queue and redrive policy.