1

i'm trying to get information for NetworkIn and NetworkOut metrics with boto3 client for python, using the get_metric_data method.

I am using the boto3 library and the client like this:

Client = boto3.client("cloudwatch")


Client.get_metric_data(
            MetricDataQueries=query,
            StartTime=start_time,
            EndTime=end_time,
            ScanBy="TimestampAscending",
)

The MetricDataQueries parameter is the following:

query = [
    {
        "Id": "network_metrics",
        "Expression": f"SEARCH(' {{AWS/EC2,InstanceId}} InstanceId = (i-xxx) (MetricName=\"NetworkIn\" OR \"NetworkOut\")', 'Sum')",
        "Label": "Metric for Instances",
        "Period": 86400,
        "ReturnData": True,
    }

]

The problem is that is not returning any result.

On the other hand, when i do the next query (with the same host) this brings the corrects results.

query = [
       {
            "Id": f"in_i-xxx".replace("-", "_"),
            "MetricStat": {
                "Metric": {
                    "Namespace": "AWS/EC2",
                    "MetricName": "NetworkIn",
                    "Dimensions": [{"Name": "InstanceId", "Value": i-xxx}],
                },
                "Period": 86400,
                "Stat": "Sum",
            },
            "ReturnData": True,
        },
        {
            "Id": f"out_i-xxx".replace("-", "_"),
            "MetricStat": {
                "Metric": {
                    "Namespace": "AWS/EC2",
                    "MetricName": "NetworkOut",
                    "Dimensions": [{"Name": "InstanceId", "Value": i-xxx}],
                },
                "Period": 86400,
                "Stat": "Sum",
            },
            "ReturnData": True,
        },

]

At the beginning i think that the first syntax was wrong, but when i use another host like i-yyy both queries brings the correct results, anyone knowns why the first syntax do not work for some hosts?

I try to do both queries with different hosts and for some the result was correct for both queries and for other hosts the first query didn't work, the second query always work.

1
  • Try starting the instances for which the first query is not working. This should populate CloudWatch Metrics with the instances data. Commented May 8 at 13:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.