List all SQS queues in an AWS account

How to list all SQS queues in an AWS account programmatically through the API and .Net SDK?

I already do something similar with DynamoDb tables, and it's pretty simple - you can send results using ListTables in a loop until you have them.

However, the equivalent endpoint of SQS Api, ListQueues is different and not so useful. It returns up to 1000 queues, without the possibility of paging.

Yes, in my case there can be more than 1000 queues. I had a query that returns exactly 1000 results. All this is in 1 region, so this is not the same as this question .

+4
source share
1 answer

You can get SQS queue names from Cloudwatch that support paging. It will only return queues that are considered active.

The active queue is described as :

The queue is considered active by CloudWatch for up to six hours from the last action (for example, any API call) in the queue.

Something like this should work:

var client = new AmazonCloudWatchClient(RegionEndpoint.EUWest1);
string nextToken = null;
var results = Enumerable.Empty<string>();

do
{
    var result = client.ListMetrics(new ListMetricsRequest()
    {
        MetricName = "ApproximateAgeOfOldestMessage",
        NextToken = nextToken
    });

    results = results.Concat(
        result
        .Metrics
        .SelectMany(x => x.Dimensions.Where(d => d.Name == "QueueName")
        .Select(d => d.Value))
    );

    nextToken = result.NextToken;

} while (nextToken != null);
+4
source

Source: https://habr.com/ru/post/1659397/


All Articles