0
votes

From my local aws-cli i can get attributes and get queueUrl of a given queue. Whereas I am not able to get that function working from an adhoc spring application. This is just a trial code. We do have config to get connected to AWS SQS. Can someone help me on this?

Flow looks like- Application A(On-prem application) calls Credential Service to get token details which is having assume role set up. Application A then uses that token details to connect to AWS and read from SQS. I am able to connect to AWS but when trying to do getUrl getting exception.

Role allowance in AWS-

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "sqs:" ], "Resource": "", "Effect": "Allow" } ] }

Working command-

aws sqs get-queue-attributes --queue-url --attribute-names All --region us-east-1

Sample Java Code-

 AmazonSQS sqs = AmazonSQSClientBuilder.standard()
           .withCredentials(new AWSStaticCredentialsProvider(credentials))      
           .withRegion(Regions.US_EAST_1).build();

    String queueUrl = "";
    try {

        queueUrl = sqs.getQueueUrl("queue-name").getQueueUrl();
    } catch (Exception e) {
        System.out.println(e);
    }

    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl).withWaitTimeSeconds(10)
            .withMaxNumberOfMessages(10);
    List<Message> sqsMessages = sqs.receiveMessage(receiveMessageRequest).getMessages();
    for (Message message : sqsMessages) {
        System.out.println("Received vice message from sqs - " + message.getBody() + ". Message ReceiptHandle - "
                + message.getReceiptHandle());

    }

Exception receiving-

An error occurred (AWS.SimpleQueueService.NonExistentQueue) when calling the GetQueueUrl operation: The specified queue does not exist or you do not have access to it.

1

1 Answers

0
votes

The line that fails is sqs.getQueueUrl("queue-name").getQueueUrl(); this API will return the URL of a queue named "queue-name". Is there a queue with that name under that region?
The aws-cli command you mentioned is not syntacticly correct. --queue-url should be followed by the queue url (Note that get-queue-attribute is part of the command, and --queue-url is a parameter for that command). This is not a request for the queue URL (as the Java code is), but a request for a queue attribute.
a cli command matching the Java code would be aws sqs get-queue-url --queue-name queue-name