Menu

Sponsored By: Password Angel - Share passwords, API keys, credentials and more with secure, single-use links.

SQS Queue: How many messages have been received?

To obtain the number of messages received (Metric = NumberOfMessagesReceived) by an Amazon SQS queue for a week using the CloudWatch API and AWS CLI, you can use the following command:

Syntax

aws cloudwatch get-metric-statistics \
  --namespace AWS/SQS \
  --metric-name NumberOfMessagesReceived \
  --dimensions Name=QueueName,Value=<QUEUE_NAME> \
  --start-time 2022-09-20T00:00:00Z \
  --end-time 2022-09-28T00:00:00Z \
  --period 86400 \
  --statistics Sum \
  --output json 

Result

Make sure to replace <QUEUE_NAME> with the name of the SQS queue you want to query. This command retrieves the sum of the NumberOfMessagesReceived metric for the specified queue every 24 hours over the specified time range and the output will resemble something like

{
    "Label": "NumberOfMessagesReceived",
    "Datapoints": [
        {
            "Timestamp": "2022-09-24T00:00:00+00:00",
            "Sum": 48.0,
            "Unit": "Count"
        },
        {
            "Timestamp": "2022-09-27T00:00:00+00:00",
            "Sum": 144.0,
            "Unit": "Count"
        },
        {
            "Timestamp": "2022-09-23T00:00:00+00:00",
            "Sum": 51.0,
            "Unit": "Count"
        },
        {
            "Timestamp": "2022-09-26T00:00:00+00:00",
            "Sum": 2094.0,
            "Unit": "Count"
        },
        {
            "Timestamp": "2022-09-22T00:00:00+00:00",
            "Sum": 0.0,
            "Unit": "Count"
        },
        {
            "Timestamp": "2022-09-25T00:00:00+00:00",
            "Sum": 334.0,
            "Unit": "Count"
        }
    ]
}

Note that the results are not necessarily in chronological order so if you need them ordered then you will need to sort the results in code.

If you receive something like

{
    "Label": "NumberOfMessagesReceived",
    "Datapoints": []
}

Then you likely have got the --metric-name value incorrect, or you are missing the argument completely. I originally omitted the --dimensions argument hoping to get the data for all queues but ended up with the empty response above instead.

If you wish to do this regularly i.e. at the beginning of each month you want to the number of messages for a queue for the last month, then you may want something more dynamic like

aws cloudwatch get-metric-statistics \
--namespace AWS/SQS \
--metric-name NumberOfMessagesReceived \
--dimensions Name=QueueName,Value=<QUEUE_NAME> \
--start-time $(date -u -d "-1 month" '+%Y-%m-%dT%H:%M:%SZ') \
--end-time $(date -u '+%Y-%m-%dT%H:%M:%SZ') \
--period 86400 \
--statistics Sum \
--output json

This command uses the date command to dynamically generate the start-time and end-time parameters based on the current time where the start-time is set to one month ago from the current time, and the end-time is set to the current time.

Enjoyed this article?

Thank you for reading this article! If you found the information valuable and enjoyed your time here, consider supporting my work by buying me a coffee. Your contribution helps fuel more content like this. Cheers to shared knowledge and caffeine-fueled inspiration!

Buy me a coffee

Originally published at https://chrisshennan.com/blog/aws-sqs-queue-how-many-messages-have-been-received

Subscribe to my newsletter...

... and receive the musings of an aspiring #indiehacker directly to your inbox once a month.

These musings will encompass a range of subjects such as Web Development, DevOps, Startups, Bootstrapping, #buildinpublic, SEO, personal opinions, and experiences.

I won't send you spam and you can unsubscribe at any time.