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?
CodePudding user response:
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
CodePudding user response:
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.