Search code examples
amazon-web-servicesboto3python-3.7

Using boto3/MQ to list queues on a MQ broker


I've tested the methods listed here: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/mq.html, but none return the actual queues running on the broker.

Is there a way to list the queues running on a broker using Boto3?

I'd like to get the queues so I can programmatically create dashboards e.g.

Region ap-southeast-2
Namespace AWS/AmazonMQ
Metric name MessageUnacknowledgedCount
Broker mybroker
VirtualHost /
Queue default

Solution

  • I'm not recommending this but it works in this situation. So there was no direct access to MQ so it wasn't possible to query it directly. Instead, I queried the Cloudwatch API to obtain the queue names e.g.

    client = conn.client('cloudwatch')
    response = client.list_metrics(
            MetricName = "MessageUnacknowledgedCount",
            Namespace='AWS/AmazonMQ',
            Dimensions=[
                {"Name": "Broker", "Value": "my-mq-instance"},
            ],
        )
        
    #Don't include queue names that match this pattern.
    pattern = re.compile(r'celery@.*\.celery\.pidbox$')
    
    queue_names = [
            dimension['Value']
            for metric in response['Metrics']
            for dimension in metric['Dimensions']
            if dimension['Name'] == 'Queue' and not pattern.match(dimension['Value'])
            and not dimension['Value'].endswith('dead_letter')
            and not dimension['Value'].startswith('celeryev')
        ]