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?

gefragt vor 7 Monaten511 Aufrufe
3 Antworten
0
Akzeptierte Antwort

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
beantwortet vor 7 Monaten
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
beantwortet vor 7 Monaten
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
beantwortet vor 7 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen