How to get information about items inside cloudwatch metrics?

334 Views Asked by At

I've created a table in cloudwatch and I would like to insert a new item only if all the same three fields weren't inserted in the last hour. Here is the current status of my table: Current status of the metric

I've inserted the information using Python and put_metric_data API:

cloudwatch = boto3.client('cloudwatch')
    response = cloudwatch.put_metric_data(
            MetricData=[
                {
                    'MetricName': 'Counter',
                    'Dimensions': [
                        {
                            'Name': 'Name',
                            'Value': "Bob"
                        },
                        {
                            'Name': 'accountId',
                            'Value': "222"
                        },
                        {
                            'Name': 'Pet',
                            'Value': "Cat"
                        },
                    ],
                    'Unit': 'Count',
                    'Value': 1
                },
            ],
            Namespace='Test_table'
        )

Once a new entry arrives, I'm trying to check if it already been inserted in the last 60 minutes time frame using get_metric_data with no success so far:

response = cloudwatch.get_metric_data(
     MetricDataQueries=[
         {
             "Id": "my_id",
             "MetricStat": {
                 "Metric": {
                     "Namespace": "Test_table",
                     "MetricName": "Counter",
                     "Dimensions": [
                         {
                             "Name": "Name",
                             "Value": "first_name"
                         },
                         {
                             "Name": "accountId",
                             "Value": "Bank_id"
                         },
                         {
                             "Name": "Pet",
                             "Value": "Pet_type"
                         }
                     ]
                 },
                 "Period": 300,
                 "Stat": "Sum",
                 "Unit": "None"
             },
         },
     ],
     StartTime=datetime.datetime.now() - datetime.timedelta(hours=24),
     EndTime=datetime.datetime.now(),
 )

What am I doing wrong here? Is there a more efficient way of doing it?

1

There are 1 best solutions below

3
Dejan Peretin On

Looks like a unit mismatch, you're publishing Count but requesting None.