Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting SQS queue name or URL from ARN, or check a queue exists by ARN

The AWS documentation states consistently that ARNs should not be constructed programmatically from names or URLs, because the way those strings are constructed is not guaranteed to be constant in time.

My issue is that, on SQS, the RedrivePolicy attribute returned by GetQueueAttributes references the dead-letter queue by ARN only.

I am currently writing a service to create queues and set them up, or verify that their setup is correct if they already exist. But I don't see the way I can verify that the dead-letter queue ARN matches an existing queue, unless I do parse it to get the name. Is there a way around that?

(actually to be fair, there is one way that respects the "don't parse ARNs programmatically" rule, which consists in calling ListQueues then loop through the resulting URLs calling GetQueueAttributes on each, but that sounds like a silly amount of work, and could potentially fail if there are more than 1000 queues on the account, so I'm excluding doing this).

Currently looking for a solution in C# but the issue is not language-dependent.

like image 502
Evren Kuzucuoglu Avatar asked Sep 10 '18 13:09

Evren Kuzucuoglu


People also ask

How do I find the URL of a SQS queue?

Returns the URL of an existing Amazon SQS queue. To access a queue that belongs to another AWS account, use the QueueOwnerAWSAccountId parameter to specify the account ID of the queue's owner. The queue's owner must grant you permission to access the queue.

Can you query an SQS queue?

It is not possible to 'search' messages in an Amazon SQS queue. A ReceiveMessage() call will retrieve one or more messages, but there is no ability to control which messages will be returned. While Message Attributes can be attached to a message, they cannot be specified when retrieving messages.

What is ARN for SQS?

In Amazon SQS, the only resource is the queue. In a policy, use an Amazon Resource Name (ARN) to identify the resource that the policy applies to. The following resource has a unique ARN associated with it: Resource type.


1 Answers

In C#, there is a parser class in the AWSSDK.Core package called Amazon.Arn.

It was seemingly added around version 3.3.104 of the package in Dec '19 (Source here). So even though ARNs aren't meant to be constructed programmatically, this seems to be setting the format in stone.

Now to get the name, knowing that the ARN is that of a queue, one can do Arn.Parse(queueArn).Resource.

Conversely, one can create a new Arn object then call ToString() on it to get the full ARN.

This of course can be improved for a random ARN (The Arn object contains more information than the resource sur as the service), and will not work as is for any resource type (e.g. Resource would return queue-name:some-guid for an SQS subscription, or rule/rule-name for an EventBridge rule.

More information can be found about ARNs here.

like image 77
Evren Kuzucuoglu Avatar answered Sep 19 '22 11:09

Evren Kuzucuoglu