0

I'm trying to write a bash script for creating an AWS SQS queue and then check that the queue is empty. For that I created the following script that should return "null":

aws sqs create-queue --queue-name MessagesQueue

queue_url=$(aws sqs list-queues --query QueueUrls[0])

queue_check=$(aws sqs get_queue_attributes --queue_url $queue_url --attribute-names ApproximateNumberOfMessages --query Attributes[0].ApproximateNumberOfMessages

And I receive this error message instead:

An error occurred (InvalidAddress) when calling the GetQueueAttributes operation: The address "https://eu-west-3.queue.amazonaws.com/XXXXXXXXXXXX/MessagesQueue" is not valid for this endpoint.

But if I explicitly write the same address in the command instead of using $queue_url it works fine and returns 'null' as it should.

Why isn't it accepting $queue_url but accepts the same URL address if I explicitly write it?

Edit: It seems like the aws-cli command reads the variable $queue_url as the URL between single and double-quotes, and when I write it explicitly it reads the URL with no quotes so that's why it accepts it. How can I use the bash variable I created so the aws-cli reads it with no quotes?

Doraemon
  • 315
  • 1
  • 10
  • Add the `set -x` command at the beginning, so bash will print an execution trace as it runs, and see what's different from when it works. I don't know if it's the problem, but I would recommend putting double-quotes around `$queue_url` to avoid weird parsing problems (variable references should almost always be double-quoted). – Gordon Davisson Nov 04 '21 at 10:14
  • @GordonDavisson I tried adding the `set -x` and apparently the only difference is that when I use `$queue_url` (or `"$queue_url"`) it shows the URL between single and double-quotes and if I write it explicitly it shows it with no quotes at all, so that might be the problem, how can I use the variable so the aws-cli interprets it as just the url with no quotes? – Doraemon Nov 04 '21 at 10:39

2 Answers2

0

Once I tried to run your code, part by part (started with aws sqs list_queues ), I have noticed:

aws: error: argument operation: Invalid choice, valid choices are:
...
Invalid choice: 'list_queues', maybe you meant:
  * list-queues

Try out list-queues instead list_queues and let know how it goes.

ouflak
  • 2,458
  • 10
  • 44
  • 49
irvis
  • 136
  • 1
  • 6
  • Yes, my bad, I copied it wrong, but in the code was well written, I have already edit it in the question – Doraemon Nov 04 '21 at 11:07
0

I solved it, I just had to remove the quotes from the output and I did it like this:

queue_url=$(aws sqs list-queues --query QueueUrls[0])
queue_url="${queue_url%\"}"
queue_url="${queue_url#\"}"

This other stackoverflow question helped: Shell script - remove first and last quote (") from a variable

Doraemon
  • 315
  • 1
  • 10