Cloudwatch boto3 get_metric_data

0

I am trying to use boto get_metric_data, but the resposne is empty:

Enter image description here

my code:

import boto3
from datetime import datetime

client = boto3.client("cloudwatch", region_name='us-east-1')

response = client.get_metric_data(
    MetricDataQueries=[
        {
            'Id': 'id',
            'MetricStat': {
                'Metric': {
                    'Namespace': 'MWAA',
                    'MetricName': 'MemoryUtilization',
                    'Dimensions': [
                        {
                            'Name': 'Environment',
                            'Value': 'Airflow-dev'
                        },
                        {
                            'Name': 'Cluster',
                            'Value': 'WebServer'
                        }
                    ]
                },
                'Period': 300,
                'Stat': 'Maximum',
            },
            'ReturnData': True
        },
    ],
    StartTime=datetime(2023, 10, 3),
    EndTime=datetime(2023, 10, 5)
)

print(response)

response:

{'MetricDataResults': [{'Id': 'id', 'Label': 'MemoryUtilization', 'Timestamps': [], 'Values': [], 'StatusCode': 'Complete'}], 'Messages': [], 'ResponseMetadata': {'RequestId': '174f4466-4381-4f73-b263-025b8cf24d40', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '174f4466-4381-4f73-b263-025b8cf24d40', 'content-type': 'text/xml', 'content-length': '500', 'date': 'Wed, 04 Oct 2023 10:03:25 GMT'}, 'RetryAttempts': 0}}

Any Ideas what I am doing wrong?

asked 9 months ago588 views
3 Answers
0
Accepted Answer

Oh sorry had overlooked it was a standard namespace. AWS namespaces are prefixed with AWS, your namespace is Namespace='AWS/MWAA', not Namespace='MWAA'.

Can you try this?

import boto3

client = boto3.client("cloudwatch", region_name='us-east-1')

list_resp = client.list_metrics(
    Namespace='AWS/MWAA')
print(len(list_resp["Metrics"]))

profile pictureAWS
Jsc
answered 9 months ago
0

Hi, is it code you're trying to run in a lambda? Are you sure you have set all the dimensions and are querying the right region? Overall the syntax looks OK. Did you try calling the list_metrics API beforehand? This would help you check for any dimensions/names mismatches.

For example (note that region is optional, if you are calling from a lambda it inherits by default the lambda region) - note this example uses the lambda_handler as I tested it from a lambda, you may not need it if you're calling your python script in a different way:

import boto3
from datetime import datetime

client = boto3.client("cloudwatch")

def lambda_handler(event, context):

    list_resp = client.list_metrics(
    Namespace='MWAA')
    print(list_resp)

profile pictureAWS
Jsc
answered 9 months ago
0

Enter image description here

import boto3

client = boto3.client("cloudwatch", region_name='us-east-1')

list_resp = client.list_metrics(
    Namespace='MWAA')
print(len(list_resp["Metrics"]))

-- 0

and

import boto3

client = boto3.client("cloudwatch", region_name='us-east-1')

list_resp = client.list_metrics(
    Namespace='AmazonMWAA')
print(len(list_resp["Metrics"]))

-- 499
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions