SNS not publishing message to SQS

0

Using publishMessage() method, I am able to create dynamic topics, subscribing with dynamic queues, but sns not publishing messsage to sqs. What is the issues here?

public void publishMessage(String topicName, String message) {
        String topicArn = createTopic(topicName);
        subSQS(topicArn, topicName);
        pubTopic(message, topicArn);
    }

    private String createTopic(String topicName) {
        CreateTopicResponse result = null;
        CreateTopicRequest request = CreateTopicRequest.builder()
                .name(topicName)
                .build();

        result = snsClient.createTopic(request);
        return result.topicArn();
    }

    private void pubTopic(String message, String topicArn) {

        PublishRequest request = PublishRequest.builder()
                .message(message)
                .topicArn(topicArn)
                .build();

        PublishResponse result = this.snsClient.publish(request);
        log.info(result.messageId() + " Message sent. Status is " + result.sdkHttpResponse().statusCode());
    }

    private void subSQS(String topicArn, String queueName) {
        
        CreateQueueRequest createQueueRequest = CreateQueueRequest.builder().queueName(queueName).build();
        String queueUrl = sqsClient.createQueue(createQueueRequest).queueUrl();

        // Get the ARN of the SQS queue
        GetQueueAttributesRequest getQueueAttributesRequest = GetQueueAttributesRequest.builder()
                .queueUrl(queueUrl)
                .attributeNames( QueueAttributeName.QUEUE_ARN)
                .build();
        GetQueueAttributesResponse queueAttributes = sqsClient.getQueueAttributes(getQueueAttributesRequest);
        String queueArn = queueAttributes.attributes().get(QueueAttributeName.QUEUE_ARN);
        
        try {
            SubscribeRequest request = SubscribeRequest.builder()
                    .protocol("sqs")
                    .endpoint(queueArn)
                    .returnSubscriptionArn(true)
                    .topicArn(topicArn)
                    .build();

            SubscribeResponse result = snsClient.subscribe(request);
            System.out.println("Subscription ARN: " + result.subscriptionArn() + "\n\n Status is " + result.sdkHttpResponse().statusCode());
        } catch (SnsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
1 Answer
0

Hello.

Have you set the SQS access policy to allow access from SNS?
You must have a policy in place as described in the document below.
https://docs.aws.amazon.com/sns/latest/dg/subscribe-sqs-queue-to-sns-topic.html#SendMessageToSQS.sqs.permissions

{
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "sns.amazonaws.com"
      },
      "Action": "sqs:SendMessage",
      "Resource": "arn:aws:sqs:us-east-2:123456789012:MyQueue",
      "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "arn:aws:sns:us-east-2:123456789012:MyTopic"
        }
      }
    }
  ]
}
profile picture
EXPERT
answered 3 months ago
profile picture
EXPERT
reviewed 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions